Forum Moderators: open

Message Too Old, No Replies

Form Validation Using Regular Expressions

         

refhat

5:35 am on Jul 28, 2010 (gmt 0)

10+ Year Member



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

daveVk

1:48 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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;
}

The red semicolons should go, if think they are acting as null statement to execute when if condition true ?

refhat

9:19 pm on Jul 28, 2010 (gmt 0)

10+ Year Member



I removed the same, but still doesn't work. when I pasted your code, It removes the validation of the other form elements like radio buttons and check boxes as well. I wonder where is the poison in the above two expressions that kills my rest of the script.

Thanks

daveVk

3:53 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also a missing ) on last if

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;
}