Javascript Form Validation

The most popular way to receive input from a user is form. Javascript plays major role in form validation. It adds the ability to validate HTML form element's input for data requirement and data formating. Javascript validate the user input on client side before sending it to the server, as a result validation by Javascript can be faster.

Require Fields Validation

The most common mistake made by user is to leave a form field blank, where input is required. In HTML form, require fields validation check wether user enter or filled all the require fields or not.

Javascript Require Fields Validation Example

<html>
<head>
<script type="text/javascript">
function validateForm()
{
	var error_msg = '';
	var name = document.getElementById('fname').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	var address = document.getElementById('address').value;
	var zip = document.getElementById('zip').value;
	
	if(name.length == 0)
		error_msg = 'Name is required';
	if(email.length == 0)
		error_msg += '\nEmail is required';
	if(phone.length == 0)
		error_msg += '\nPhone is required';
	if(address.length == 0)
		error_msg += '\nAddress is required';
	if(error_msg.length > 0)
		alert (error_msg);
	return false;
}
</script>
</head>
<body>
<form>
Name: <input type="text" id="name">
Email: <input type="text" id="email">
Phone: <input type="text" id="phone">
Address: <input type="text" id="address">
Zip: <input type="text" id="zip">
<input type='button' onclick="validateForm()" value="Submit" />
</form>
</body>
</html>
Name:
Email:
Phone:
Address:
Zip:
 

Data Format Validation

Once user filled all the required fields and submit the form, we need to check format of the value submited by user is correct or not. To check format of input data, Javascript allow us to use regular expression as per the data format require for field. Below are the few example of regular expression frequently use for data format validation.

Syntax

Email Format -> /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
Only Alphabets - /^[a-zA-Z ]*$/
Only Alphanumeric - /^[ A-Za-z0-9_@'.\/#&+,-\][()]*$/
Zip Code - /(^\d{5}$)|(^\(?(\d{5})[- ]?(\d{4})$)/
Phone - /^((\d{3}-\d{4})|((\d{3}\s){0,1}\d{3}\s\d{4})|((\d{3}\.){0,1}\d{3}\.\d{4})|((\d{3}\-){0,1}\d{3}\-\d{4})|((\(\d{3}\) ){0,1}\d{3}-\d{4}))$/
AreaCode - /^[0-9]{3}$/

Javascript Data Format Validation Example

<html>
<head>
<script type="text/javascript">
function validateDataFormat()
{
	var error_msg = '';
	email_val = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	alphabets_val = /^[a-zA-Z ]*$/;
	zip_val = /(^\d{5}$)|(^\(?(\d{5})[- ]?(\d{4})$)/;
	phone_val = /^((\d{3}\-){0,1}\d{3}\-\d{4})$/;

	var name = document.getElementById('fname1').value;
	var email = document.getElementById('email1').value;
	var phone = document.getElementById('phone1').value;
	var address = document.getElementById('address1').value;
	var zip = document.getElementById('zip1').value;

	if(name.length == 0)
		error_msg = 'Name is required';
	else if(!alphabets_val.test(name)) 
		error_msg = '\nPlease insert valid name';
	if(email.length == 0)
		error_msg += '\nEmail is required';
	else if(!email_val.test(email))
		error_msg += '\nPlease insert valid email';
	if(phone.length == 0)
		error_msg += '\nPhone is required';
	else if(!phone_val.test(phone))
		error_msg += '\nPlease insert valid phone number';
	if(address.length == 0)
		error_msg += '\nAddress is required';
	if((!zip_val.test(zip)) && zip.length > 0)
		error_msg += '\nPlease insert valid zip code';
		
	if(error_msg.length > 0)
		alert (error_msg);
	return false;
}
</script>
</form>
</head>
<body>
<form>
<table border="0" cellpadding="5" cellspacing="5" class="setBorder" width="50%">
<tr><td>Name: </td><td><input type="text" id="fname1"></td></tr>
<tr><td>Email: </td><td><input type="text" id="email1"></td></tr>
<tr><td>Phone: </td><td><input type="text" id="phone1"></td></tr>
<tr><td>Address: </td><td><input type="text" id="address1"></td></tr>
<tr><td>Zip: </td><td><input type="text" id="zip1"></td></tr>
<tr><td> </td><td><input type='button' onclick="validateDataFormat()" value="Submit" /></td></tr>
</td></tr>
</table>
</body>
</html>
Name:
Email:
Phone:
Address:
Zip: