Javascript form validation
Easy and simple code of javascript form validation shown below:Code :
<script type="text/javascript">
function validate()
{
var error="";
var status= true;
var emailpattern = /^[A-Za-z0-9\-\.\_]+\@[A-Za-z0-9\-\.]+\.[a-zA-Z]+$/;
if (document.f1.name.value == "")
{
error += "Please enter your name!!!\n";
status = false;
document.f1.name.focus();
}
if (document.f1.email.value == "")
{
error += "Please enter your email Address\n";
status = false;
document.f1.email.focus();
}
if (document.f1.phone.value == "")
{
error += "Please enter Phone Number\n";
status = false;
document.f1.phone.focus();
}
if (document.f1.pwd.value == "")
{
error += "Please enter Password\n";
status = false;
document.f1.pwd.focus();
}
if (document.f1.cpwd.value == "")
{
error += "Please retype your Password\n";
status = false;
document.f1.cpwd.focus();
}
if (!emailpattern.test(document.f1.email.value))
{
error += "Enter a valid email address \n";
status = false;
document.f1.email.focus();
}
if (status == false)
{
alert(error);
return false;
}
else
{
return true;
}
}
</script>
<html>
<head>
<title>phpedu108 tutorial</title>
</head>
<body>
<h2>This is a Form Validation Demo</h2>
<div>More tutorials <a href="http://phpedu108.blogspot.com" style="color:#0066CC; font-weight:bold">http://phpedu108.blogspot.com</a> </div>
<form action="formValid.html" onSubmit="return validate()" name="f1">
<table>
<tr><td>Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>email:</td><td><input type="text" name="email"></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone"></td></tr>
<tr><td>Password:</td><td><input type="Password" name="pwd"></td></tr>
<tr><td>Confirm Password:</td><td><input type="Password" name="cpwd"></td></tr>
<tr><td><input type="Submit" Value="Submit me!!!"></td></tr>
</table>
</form>
</body>
</html>
No comments:
Post a Comment