JavaScript form validation

Form validation is the way of insure that form data entered by the user meets the specified criteria. Let us take the example of a registration form which needs name, email id etc. We want that name should not contains any special character and email address should be a valid one like with format [email protected] So for such kind of checks we can validate the form.

Javascript serves the purpose of validating a HTML form to check for any inappropriate values or any empty field for the form fields like name, password, email, date, mobile number etc.


<!DOCTYPE html>
function validateForm() {
	var x = document.forms["Form"]["name"].value;
	var y = document.forms["Form"]["password"].value;
	if (x == "") {
    	alert("Name must be filled out.");
    	return false;
	else if (y.length < 8)
    	alert("Password must be at least 8 characters long.");
    	return false;
<form name="Form" action="/action_page.php"
onsubmit="return validateForm()" method="post">
Name: <input type="text" name="name">
Password: <input type="text" name="password">
<input type="submit" value="Submit">

Related Topics

Content Protection by
Please Share