Form Validation Script

by Hiroshi on June 13, 2008

in Form Validation

Form Authentication or Validation

Give your form a name and put onsubmit action in form tag. i.e.

<form name="mailform" method="POST" action="pagename.php" onsubmit="return CheckData()">

Put this in the head section of your webpage (Authentication java script). You can also create an external javascript file lets say abc.js and call it in head section of the webpage by this.

<script type="text/javascript" src="abc.js"></script>

This approach is more recommended by calling javascript file from outside.

<script language="javascript">
function isValidEmail(str) {
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function CheckData()
{
with(document.mailform)
{
if(name.value == "")
{
alert("Please enter your name.");
name.focus();
return false;
}
 
if(company.value == "")
{
alert("Please enter your company name.");
company.focus();
return false;
}
 
if(address.value == "")
{
alert("Please enter your address.");
address.focus();
return false;
}
 
if(phone.value == "")
{
alert("Please enter your phone.");
phone.focus();
phone.value="";
return false;
}
 
if(cell.value == "")
{
alert("Please enter your cell.");
cell.focus();
phone.value="";
return false;
}
 
if(email.value == "")
{
alert("Please enter your email address.");
email.focus();
email.value="";
return false;
}
if (!isValidEmail(email.value))
{
alert("Please enter a valid email address");
email.focus();
email.value="";
return false;
}
 
if(find.value == "")
{
alert("Please tell me where did u find me.");
find.focus();
return false;
}
 
if(comments.value == "")
{
alert("Please enter Message/Query.");
comments.focus();
return false;
}
}
return true;
}
</script>

For email and phone or cell you might want to focus the field and at returning false might remove values written by user and focus that field. For that use this before return false;

email.focus();
email.value="";

Form Validation or Form Authentication Script Download

Related Posts

Previous post:

Next post: