w3resource

JavaScript: HTML Form Validation

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 :

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.

Previous: Javascript : Java object, JavaArray, JavaClass, JavaObject, JavaPackage, Packages, sun, netscape
Next: JavaScript: HTML Form - checking for non empty field

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Memoized function

const add = () => {
  const cache = {};
  return num => {
    if (num in cache) {
      return 'From cache! ${cache[num]}';
    } else {
      const result = num + 10;
      cache[num] = result;
      return 'Calculated! ${result}';
    }
  };
};

const addFunction = add();
console.log(addFunction(10));
console.log(addFunction(10));
console.log(addFunction(5 * 2));

The add function is a memoized function. With memoization, we can cache the results of a function in order to speed up its execution. In this case, we create a cache object that stores the previously returned values.
If we call the addFunction function again with the same argument, it first checks whether it has already gotten that value in its cache. If that's the case, the caches value will be returned, which saves on execution time. Else, if it's not cached, it will calculate the value and store it afterwards.
We call the addFunction function three times with the same value: on the first invocation, the value of the function when num is equal to 10 isn't cached yet. The condition of the if-statement num in cache returns false, and the else block gets executed: Calculated! 20 gets logged, and the value of the result gets added to the cache object. cache now looks like { 10: 20 }.
The second time, the cache object contains the value that gets returned for 10. The condition of the if-statement num in cache returns true, and 'From cache! 20' gets logged.
The third time, we pass 5 * 2 to the function which gets evaluated to 10. The cache object contains the value that gets returned for 10. The condition of the if-statement num in cache returns true, and 'From cache! 20' gets logged.

Ref: https://bit.ly/3jFRBje