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..


Reply With Quote

Bookmarks