Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,916
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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.

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I need to prevent only dots.
    Thanks for ur quick reply

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • Users who have thanked Philip M for this post:

    arunkumar93 (09-28-2010)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,916
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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.

  • #6
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    The code shud PREVENT the below format also:

    .
    .b
    .6
    .=
    .$

    Thanks & Regards
    Arun

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • #8
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for ur Code.
    Actually i have another onkeypress function to prevent all other special characters except dots.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,916
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    *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.
    Last edited by Old Pedant; 09-28-2010 at 09:20 PM.
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • #11
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Philip,

    Can you explain these two lines for me?

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

    Thanks & Regards
    Arun

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,916
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    As you say, DOH! I misread it as var regex = /(^\s+\.|\.\s+)/; Sorry.

  • #15
    New Coder
    Join Date
    Sep 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •