You are on page 1of 2

Form Validation.

Many Web Sites show how to validate forms. All of them have some small peaces of
information. Usefull information but it is not completed. I will try to show as much as
possible about form validation. Try this:
Enter email address:
Enter any Number:
Enter Date:
Enter at least 3 characters:
Enter no more then 3
characters:
Enter Anything:

In order to validate form you have to call function that I called formVal():
<form method="POST" action="default.asp" name="form1" onSubmit="return
formCheck()">
Into the HEAD section:
<script language="JavaScript"> function formCheck()
{
.....Validation rules here........
.........
........
}
</script>
Now what kind of validation rules we might use:
1. Email Validation:
if (document.form1.email.value.indexOf("@") == -1 ||
document.form1.email.value == "")
{
alert("Please enter email address.");
document.form1.email.focus();
return false;
}
2. Number Validation:
if( isNaN(document.form1.number.value) )
{
alert('Not a number!');
document.form1.number.focus();
return false;
}
3. Date Validation:
var checkdt=new Date(Date.parse(document.form1.date.value ) );
if( !checkdt.getYear() )
{
alert('Not a date!');
document.form1.date.focus();
return false;
}
4. Lenght Validation:
No longer then:
if (document.form1.field1.value.length>3)
{
alert("Do not insert more then 3 characters");
document.form1.field1.focus();
return false;
}
Longer then:
if (document.form1.field2.value.length<3)
{
alert("Do not insert less then 3 characters");
document.form1.field2.focus();
return false;
}
5. Not Empty field Validation:
if (document.form1.anything.value == "")
{
alert("Sorry but field Anything cannot be empty.");
document.form1.anything.focus();
return false;
}

You might also like