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.
Results 1 to 5 of 5
  1. #1
    pml
    pml is offline
    New Coder
    Join Date
    Mar 2005
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript: Validation problems. Need to check for dots in form.

    I have a form with three textboxes that I need to check that they don’t contain any dots (“.”). Thus the following combinations, for example, should return false (3434.3356) (.sdfsah.sdlfsdf) (pdhhpp.4235) All strings that contain a dot (or several) in any way should return false. How can I check for that? I have this code already and would appreciate if you could add to that, because I don’t know how to interpret and read regular expressions.


    function check(){

    if(document.form1.text1.value==""){ alert("You have a blank box"); return false; }
    else if
    // here I don’t know how to continue to check that there are no dots in the text box.


    else
    { return true; }
    }

  • #2
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,703
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there pml,

    try this...
    Code:
    <script type="text/javascript">
    <!--
    function check() {
    
      var pattern=document.form1.text1.value;
    
    if(pattern=="") { 
       alert("You have a blank box"); return false; 
     }
    else {
    if(pattern.search(/\./i )!=-1) { 
       alert("You have a dot in the box"); return false;
     }
    else { 
       return true; 
       }
      }
     }
    //-->
    </script>
    coothead

  • #3
    pml
    pml is offline
    New Coder
    Join Date
    Mar 2005
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks!

    Thank you for your very useful reply Coothead! It works perfectly fine now. However, even though I know what the reg.exp. does, I was just wondering if you coul explain how it functions. Basically I understand the code to the period in the middle but then the reg.exp. is closed with the "/", or am I wrong. Thereafter I don't know the function of "i" and the end I guess means not equal to -1. I just don't get all of it...and therefore it doesn't completely make sense. I would of course be happy if you could take a few seconds and explain it all to me...

    pattern.search(/\./i )!=-1)

    Thanks!

    Pml

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    !=-1 means "if the search returns a value not equal to -1", which in turns means that a . (period, dot) has been found at some position in the string (say at position 5 or whatever).

    OK?

    As a quibble,

    if(pattern=="") {
    alert("You have a blank box"); return false;
    }

    will return true if the box contains just a space character.
    It might be prudent to have an additional test:-

    if(pattern.search(/^\s+/i )!=-1) {
    alert("You have a blank box"); return false;
    }


    i.e if the pattern to be matched starts with a space(s)

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    The i there means case-insensitive, meaning the case (lower or upper) doesn't matter. But since the character to be matched is a dot or space, the i there is unnecessary.


  •  

    Posting Permissions

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