w3resource logo

javascript form validation

JavaScript : HTML Form Validation

Secondary Nav

What you will learn

In this tutorial, you will learn how to use JavaScript to validate an HTML form. We have discussed the following HTML Form Validation topics with examples :

w3r video tutorialsWatch JavaScript form validation video tutorial.

What is form validation?

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 JavaScript 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[0] refers to the first form of the document, document.forms[1] 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. But that is beyond the scope of this tutorial.