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 a@b.com. 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.
Example:
<!DOCTYPE html> <html> <head> <script> 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; } } </script> </head> <body> <form name="Form" action="/action_page.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="name"> Password: <input type="text" name="password"> <br><br> <input type="submit" value="Submit"> </form> </body> </html> |
Related Topics
- Javascript Email Validation
- Javascript Not Empty Validation
- JavaScript Alphabets Validation
- Javascript Alphabets & Space Validation
- Javascript Alphanumeric Validation
- Javascript String Length Validation
- Javascript Phone Number Validation
- Javascript Credit Card Validation
- Javascript IP Address Validation
Please Share