It is very important to pre-validate the forms before submission as they can have inappropriate values. JavaScript provides the facility to validate the form on the client-side so data processing will be faster than server-side validation.
function validate() {
// name can not be blank
if( document.myForm.name.value == "" ) {
alert( "Please enter your name!" );
document.myForm.name.focus();
return false;
}
// email can not be blank
if( document.myForm.email.value == "" ) {
alert( "Please enter your email!" );
document.myForm.EMail.focus();
return false;
}
// enter a password with at least 7 characters
if( document.myForm.password.value == "" || isNaN( document.myForm.password.value ) || document.myForm.password.value.length > 7 ) {
alert( "Please enter a password with at least 7 characters");
document.myForm.mobile.focus();
return false;
}
return( true );
}