www.webdeveloper.com
Results 1 to 10 of 10

Thread: JavaScript Forum Validation with regular expressions

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    36

    JavaScript Forum Validation with regular expressions

    I have a form in html that I need to validate using Javascript regular expressions.

    I was validating the fields one by one and it was working fine till I tried to validate email field. Even If I remove email validation field still the form validates Name correctly, atleast.

    Here is my code:

    Code:
    <html>
    <head>
    	<script type="text/javascript">
    	function validate()
    	{
    		if(document.getElementById('name').value== "")
    			{
    				alert("Please Fill the Name");
    				document.getElementById('name').focus();
    				return false;
    			}
    			if(!document.getElementById('phone').value.match(/^[0-9]{10}$/));
    			{
    				alert("Phone No. is not valid");
    				return false;
    			}
    			if(!document.getElementById('email').value.match(/^[a-zA-Z0-9_.-]+@[a-z0-9][a-z0-9\-]{1,64}(\.[a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})$/i);
    			{
    				alert("Email Id is not Valid");
    				return false;
    			}
    			
    		return true;
    		}
    	</script>
    </head>
    <body>
    <form name="myform" id="myform" method="get">
    <table border ="1" align="center" cellpadding="0" cellspacing="0">
    	<tr><td>Name</td><td><input type="text" id="name" name="name" maxlength="10"></td></tr>
    	<tr><td>Phone</td><td><input type="text" id="phone" name="phone"></td></tr>
    	<tr><td>Email</td><td><input type="text" id="email" name="email"></td></tr>
    	<tr><td>Gender</td><td><input type="radio" name="male" id="male">Male</input><input type="radio" name="male" id="male">Female</input></td></tr>
    	<tr><td>3 Boxes</td><td><input type="checkbox" name="box1" id="box1">Box 1</input><input type="checkbox" name="box2" id="box2">Box 2</input><input type="checkbox" name="box3" id="box3">Box 3</input></td></tr>
    	<tr><td></td><td><input type="button" value = "Submit Form" onclick="return validate();"></td></tr>
    </table>
    </form>
    </body>
    </html>

    At first the phone no. field was working correctly now it too doesn't work correctly. I am not sure what did I do wrong. can someone point me to right direction as where I am doing wrong.

    Is it incorrect to match using a ! operator , if so what would be the correct way to validate this form using Regular expressions.

    Thanks for the help..

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    if(!/^[0-9]{10}$/.test(document.getElementById('phone').value))
    Use the same test for email.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Jul 2010
    Location
    UK
    Posts
    28
    I have a more basic question about validation - can someone give me a link to a tutorial about validating input that is only numbers and letters?

    i.e. i want an input that can only be a-z/0-9.

    thanks
    Last edited by nursa; 07-28-2010 at 05:06 AM.
    "One should never increase, beyond what is necessary,
    the number of entities required to explain anything."

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Jul 2010
    Location
    UK
    Posts
    28
    thanks Fang - one more thing - do you have to use getElement to match the value, or can you just pass a string to the function?
    e.g.
    Code:
    function validate(string){
     if string.value != ""{
      if string.value.match(/^[0-9a-z]/){
       alert("blah")
    }}}
    i've tried this but i cant get it working
    Last edited by nursa; 07-28-2010 at 06:17 AM.
    "One should never increase, beyond what is necessary,
    the number of entities required to explain anything."

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    If you pass a string there is no need for value
    Code:
    function validated(string) {
    if(string != "") {
        if(string.match(/^[0-9a-z]/)) {
            alert("blah");
            }
        }
    }
    Also missing brackets for if statement
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Jul 2010
    Location
    UK
    Posts
    28
    lol thanks, i thought it was something that simple
    "One should never increase, beyond what is necessary,
    the number of entities required to explain anything."

  8. #8
    Join Date
    Jul 2010
    Location
    UK
    Posts
    28
    i had to use '.value' :

    Code:
    function validate(str) {
      alert(str.value)
      if(str.value != "") {
        alert("null check passed")
        if(str.value.match(/[^0-9a-z]/)) {
          alert("Only A-Z and 0-9 allowed!");
        }
      }
    }
    you do need it, this code works
    Last edited by nursa; 07-28-2010 at 06:43 AM.
    "One should never increase, beyond what is necessary,
    the number of entities required to explain anything."

  9. #9
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Then you are not passing a string, but a reference.
    Last edited by Fang; 07-28-2010 at 06:39 AM.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  10. #10
    Join Date
    Jul 2010
    Location
    UK
    Posts
    28
    ok cheers
    "One should never increase, beyond what is necessary,
    the number of entities required to explain anything."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles