Validation in JavaScript

This article is about form validation using JavaScript with easy examples.

Validation

Whenever users fill in a registration form in any website, there is a need for appropriate information to be filled in by the candidates. If by mistake users provide the wrong information or in the incorrect format then there should be an error indication for the user to correct his/her mistakes and then proceed further.

So, there is a need for validation by which the preceding problem can be solved. Validation is done when the form is submitted by the user.

JavaScript provides the ability to validate the form on the client side so the processing will be faster than the server-side validation. So validation is an important field and it must be done.

Using JavaScript you can validate fields for name, password, address, mobile number, email and so on.

Validation into two parts

We will explain the validation in two parts, in other words a few fields of the form and email field separately.

Validation into two parts

Form validation

Here is an example in which the validations of three fields have been done, in other words name, password and mobile number. Name cannot be empty, password should be at least 7 characters long and mobile number should be equal to 10 digits.

Here, we are validating the form on form submit and the user cannot proceed further until the given values or information are appropriate.
 

<html>

<head>

    <title>Validation in JavaScript</title>

</head>

<script>

    function formvalidate() {

        var name = document.form.name.value;

        var password = document.form.password.value;

        var contact = document.form.contact.value;

        var address = document.form.address.value;

        if (name == null || name == "") {

            alter("Please enter your name");

            return false;

        }

        else if (password.length == 0) {

            alter("Please enter your secret password");

        }

        else if (password.length < 7) {

            alter("your password should be at least 7 characters long");

            return false;

        }

        else if (contact.length == 0) {

            alter("Please enter your contact number");

        }

        else if (contact.length < 10) {

            alter("contact number should be equal to 10 digits");

            return false;

        }

        else if (address.length == 0) {

            alter("Please enter your address");

        }

    }

</script>

<body>

    <form name="form" method="post" action="xyz.jsp" onsubmit="formvalidate">

    Name:<input type="text" value=""></br> Password:<input type="password" value=""></br>

    Contact:<input type="text" value=""></br> Address:<input type="text" value=""></br>

    <input type="submit" value="Submit">

    <input type="reset" value="Reset">

    </form>

</body>

</html> 

Output

Form validation

Email Validation

There may be many criteria for validating the email such as the following.

  • Email must consists of “@” and “.” (dot) symbols in it.
  • There must be at least one character before and after “@” symbol.
  • There must be two characters after “.” Symbol.

Let's look at the example.

<html>

<head>

    <title>Validation in JavaScript</title>

</head>

<script>

function emailvalidate()

{

var x=document.form.name.value;

var atrateposition=x.indexOf("@");

var dotposition=x.lastIndexOf(".");

if(atposition<1 || dotposition<atposition+2 || atposition+2=x.length)

{

alter("Please enter your valid email \n atposition:"+atposition+"\n dotposition:+"dotposition);

return false;

}

}

</script>

<body>

    <form name="form" method="post" action="xyz.jsp" onsubmit="emailvalidate">

    Email:<input type="text" value="email"></br>

    <input type="submit" value="Submit">

    <input type="reset" value="Reset">

    </form>

</body>

</html>

Output

Email Validation 

Up Next
    Ebook Download
    View all
    Learn
    View all