To create a Java Script-enabled form that checks whether a user has filled in the form correctly or not before it's sent to the server. This is called form validation.
What is form validation?
Form validation is the process of checking that a form has been filled in correctly or not before it is processed.
For example, if your form has a box for the user to type their email address or any user id, you might want your form handler to check that they've filled in their address before you deal with the rest of the form.
There are two methods for the form validation.
1: Client-Side validation
2: Server-side validation
1: Client-Side validation
In Java Script Client-side form validation is an important part of a web site where data needs to be collected from the user. Users are innately ignorant, and will mess up data entry in a web form if given the chance. It is the job of the web programmer, then, to make sure his pages which use forms include client-side form validation using JavaScript.
In Java Script the server also benefits from client-side validation since it saves a number of round-trips between the visitor and the server owing to typos and easily spotted mistakes. This advantage does not alleviate the neccessity of doing server-side validation.
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var username = document.form1.username.value;
var phone = document.form1.phone.value;
var passA = document.form1.passwordA.value;
var passB = document.form1.passwordB.value;
if(!validateRequired(username))
{
alert("Username required");
return false;
}
if(!isValid(phone))
{
alert("Phone must be numerical");
return false;
}
if(!bothSame(passA, passB))
{
alert("Passwords must match");
return false;
}
return true;
}