In this tutorial, you will learn how to use Java Script to validate an HTML form.
We have discussed the following HTML Form Validation topics with examples : video_library Watch Java Script form validation video tutorial. Form validation is the process of making sure that data supplied by the user using a form, meets the criteria set for collecting data from the user.
For example, if you are using a registration form, and you want your user to submit name, email id and address, you must use a code (in Java Script or in any other language) to check whether the user entered a name containing alphabets only, a valid email address and a proper address.
Accessing form data If an HTML document contains more than one forms, they can be accessed as either by document.form_name where form_name is the value of the name attribute of the form element or by document.forms[i] where i is 0, 1,2,3....
and document.forms refers to the first form of the document, document.forms refers to the second form of the document and so on.
Elements of a form can be accessed by document.form_name.form_element where form_name is the value of the name attribute of the form element, form_element is the value of the name attribute of the form's element.
There are other ways of accessing the forms as well as form's elements also using DOM API.
The preliminary checks can be performed to check for password length, empty field, unwanted text etc.
Some validations require use of Regular Expressions for pattern matching for addresses, zip codes, phone numbers etc.
It is important to validate the form submitted by the user because it can have inappropriate values. The Java Script provides you the facility the validate the form on the client side so processing will be fast than server-side validation.
So, most of the web developers prefer Java Script form validation.
Through Java Script, we can validate name, password, email, date, mobile number etc fields.
In this example, we are going to validate the name and password.