Java Script


JavaScript Tutorial


Admission Enquiry Form


Form validation in JavaScript

JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. The form must be checked to make sure all the mandatory fields are filled.


Example of form validation

<html>
<head>
<title>Form Validation</title>
<style>
form{background-color:#CCC; width:400px;}
label{font-size:18px;}
</style>
<script>
function validation()
{
var name=document.getElementById("txt_name");
var password=document.getElementById("txt_password");
if(name.value=="")
{
name.focus();
name.style.backgroundColor="red";
return false;
}
else if(password.value=="")
{
password.focus();
password.style.backgroundColor="red";
return false;
}
else
{
return true;
}
}
</script>
</head>

<body>
<form onSubmit="return (validation())">
<table>
<tr><td><label>Enter name</label></td><td><input type="text" id="txt_name"></td></tr>
<tr><td><label>Enter password</label></td><td><input type="password" id="txt_password"></td></tr>
<tr><td></td><td><input type="submit" style="height:40px; width:80px; font-size:18px;"></td></tr>
</table>
</form>
</body>
</html>