Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2010, 08:28 PM   PM User | #1
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
How do i to filter dots in a texbox using javascript

I want to alert the user if he enters only dots without any alphabets or numerals in a textbox using javascript.
If the input contains dots in between alphabets and numerals the javascript should not alert the user.I have a javascript which alerts the user even if dots are present in between alphabets and numerals.
Can any1 Help me..Below is my script:
Code:
function addressValidation(obj)
{
var regex = new RegExp("[.]");
if(obj.value.match(regex))
{
alert("Dot is not allowed");
obj.focus();
return false;
}
}

Last edited by arunkumar93; 09-29-2010 at 06:33 PM..
arunkumar93 is offline   Reply With Quote
Old 09-28-2010, 08:33 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What about if the user enters only dashes? Or only commas? Or only equal signs???

Would
====.=====
be legal?

Would
abc=def
be legal?

Can you be more specific about what *IS* legal??
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-28-2010, 08:36 PM   PM User | #3
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
I need to prevent only dots.
Thanks for ur quick reply
arunkumar93 is offline   Reply With Quote
Old 09-28-2010, 08:42 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Like so:-

Code:
<script type = "text/javascript">

function addressValidation(obj) {
var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
// OR IF YOU PREFER obj.value = obj.value.replace(/^[^a-z0-9\.]+/i,"");  // strip leading all non-alphanumeric characters (except dots)
if(obj.value.match(regex)) {
alert("Dot is not allowed");
obj.focus();
return false;
}
}

</script>

. in a regex means "any character". A literal dot is \.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.


You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

Last edited by Philip M; 09-28-2010 at 09:02 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
arunkumar93 (09-28-2010)
Old 09-28-2010, 08:54 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Philip's trick is a neat one: After getting rid of the leading/trailing spaces, he is simply checking to see if the dot is the first character left. If it is, then it's no good.

So his code *DOES* allow

a.b
3.x
=.=
$.$

and it PREVENTS

.
.b
.6
.=
.$

But it *also* allows

a.
2.
=.
$.

Is that what you want?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-28-2010, 08:58 PM   PM User | #6
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
The code shud PREVENT the below format also:

.
.b
.6
.=
.$

Thanks & Regards
Arun
arunkumar93 is offline   Reply With Quote
Old 09-28-2010, 09:06 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
But it *also* allows

a.
2.
=.
$.

Is that what you want?
"If the input contains dots in between alphabets and numerals the javascript should not alert the user."

I have made a modification to the code but like you I am unclear if characters other than alpha-numeric are allowed. My alternative deletes them in the start position in the string.
Philip M is offline   Reply With Quote
Old 09-28-2010, 09:16 PM   PM User | #8
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
Thanks for ur Code.
Actually i have another onkeypress function to prevent all other special characters except dots.
arunkumar93 is offline   Reply With Quote
Old 09-28-2010, 09:18 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
*POSSIBLY* this would do it???
Code:
function addressValidation(obj) {
    var regex = /(^\s*\.|\.\s*$)/;
    if( regex.test( obj.value ) {
        alert("Neither leading nor trailing dot is allowed");
        obj.focus();
        return false;
    }
    return true;
}
Combined the "strip spaces" so it's all done with one regex.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 09-28-2010 at 09:20 PM..
Old Pedant is offline   Reply With Quote
Old 09-29-2010, 08:58 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
*POSSIBLY* this would do it???
Code:
function addressValidation(obj) {
    var regex = /(^\s*\.|\.\s*$)/;
    if( regex.test( obj.value ) {
        alert("Neither leading nor trailing dot is allowed");
        obj.focus();
        return false;
    }
    return true;
}

Combined the "strip spaces" so it's all done with one regex.
You are getting tired or your brain is becoming weak.

That will not block .Old Pedant (dot followed by anything except a space). As I understand it the OP's requirement is simple - no leading dot(s). Dots between alphanumeric characters are OK.

In any case there is a typo:-

if( regex.test( obj.value ) ) {

I cannot understand this passion for crowding several different tests into one regex. It simply makes it harder to test and debug.

Just stick with the code I gave you in Post#4, with the following modification if trailing as well as leading dots are not allowed:-

Code:
var regex = /^\.|\.$/;

Last edited by Philip M; 09-29-2010 at 09:12 AM..
Philip M is offline   Reply With Quote
Old 09-29-2010, 06:32 PM   PM User | #11
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
Hi Philip,

Can you explain these two lines for me?

var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,"");

Thanks & Regards
Arun
arunkumar93 is offline   Reply With Quote
Old 09-29-2010, 06:55 PM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by arunkumar93 View Post
Hi Philip,

Can you explain these two lines for me?

var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,"");

Thanks & Regards
Arun
var regex = /^\./; // ^ means "at the start of the string". \. means "a dot character". So the regex means "match a dot character at the start of the string".

obj.value = obj.value.replace(/^\s+|\s+$/g,""); // means strip (delete or replace by nothing "") all space characters (\s+) at the start (^) or (|) the end ($) of the string. The g switch means "global", that is replace all matches rather than just the first one.
Philip M is offline   Reply With Quote
Old 09-29-2010, 07:41 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Philip M View Post
That will not block .Old Pedant (dot followed by anything except a space).
?????
Adapted to just use a passed in string instead of form field:
Code:
<script>

function addressValidation(str) {
    var regex = /(^\s*\.|\.\s*$)/;
    if( regex.test( str ) ) {
        alert(str + "\nNeither leading nor trailing dot is allowed");
        return false;
    }
    alert(str + "\nOKAY");
    return true;
}

addressValidation(".Old Pedant");

addressValidation("                              .Old Pedant");

addressValidation("truly.     ");
</script>
But, yes, I did miss a right parenthesis.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-29-2010, 08:36 PM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
As you say, DOH! I misread it as var regex = /(^\s+\.|\.\s+)/; Sorry.
Philip M is offline   Reply With Quote
Old 10-03-2010, 03:40 PM   PM User | #15
arunkumar93
New Coder

 
Join Date: Sep 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
arunkumar93 is an unknown quantity at this point
Hi Philip,
Code:
<script type = "text/javascript">

function addressValidation(obj) {
var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
// OR IF YOU PREFER obj.value = obj.value.replace(/^[^a-z0-9\.]+/i,"");  // strip leading all non-alphanumeric characters (except dots)
if(obj.value.match(regex)) {
alert("Dot is not allowed");
obj.focus();
return false;
}
}

</script>
In this is code can you change regex so it allows anything which starts with dot.

.b
.6
.=
.$


Thanks & regards
Arun
arunkumar93 is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript dots filter

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:52 AM.


Advertisement
Log in to turn off these ads.