Validation in ES6

Validation in ES6

The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data provided by the front-end user is incorrect or undefined, the web server sends it back to the user. The server also requests to resubmit the form that contains the correct information.

The validation process is done at the web server end. The server sends the validation to the client. It is a time taken to process for both users as well as for the web server. ES6 provides a facility to the user to validate the data form before sending it to the web server.

There are following two functions of form validation. They are-

Basic Validation: It is a process of checking that all the essential fields are filled or not.

Data Format Validation: It helps us to make sure that the input data is correct or not. The data form validation process involves a convenient logic for data testing.

syntax

<script>
function abc()
{
          // validation code 
}
</script>
<form action=“page redirection” name=“form name” onsubmit= “return function()”>
<! - - form - - >
</form>

The above syntax contains the following parameters.

action=“page redirection”: It is used to redirect the current page to next page if the input information is correct.

Onsubmit= “return function()”: When the user clicks on the submit button, then this parameter invokes the validate function.

Name=“form name”: The name parameter helps us to access the value from the form.

Here, we will try to elaborate on each validation in detail.

Basic Form Validation

This type of form validation is used to check all essential input fields whether filled or not.

Here, we have an example to understand the above validation.

Example

<!DOCTYPE html>  
<html>  
<head>  
    <title>Basic form Validation</title>  
    <script>  
        function validateBasic()
{  
            var name1 = document.basic.name.value;  
            var cour1 = document.basic.cour.value;              
            var phone1 = document.basic.phone.value;   
            if (name1 == "")
{  
             alert("Enter Your Name"); 
                return false;    
            }  
            if (cour1 == "") {  
             alert("Enter Your Course"); 
                return false;    
            }  
            if (phone1 == "") {  
             alert("Enter Your Phone Number"); 
                return false;    
            }  
        }  
    </script>  
</head>  
<body>  
    <center>  
        <h1>Basic Form Validation</h1>   
        <form name="basic" action="#" onsubmit="return validateBasic()">  
            <table cellspacing = "2" cellpadding = "2" border = "1"> 
            <tr> 
                <td>Name: </td> 
                <td><input type="text" name="name"></td> 
            <tr> 
            <tr> 
               <td> Course: </td> 
               <td><input type="text" name="cour"></td> 
            </tr> 
            <tr> 
                <td>Phone no.:</td> 
                <td><input type="text" name="phone"></td> 
            </tr> 
        </table> 
        <input type="submit" value="Submit"> 
    </form>  
       <p id="d1"></p>  
    </center>  
</body>  
</html> 

Output

After the successful execution of the above example, we got the following output:

Validation in ES6

If we click on the submit button without entering the data in text fields, it will show the following alert message.

Validation in ES6

Data Format Validation

In the data format validation, we have to check that the input data having correct value or not. It validates the already filled input fields to check if the provided information is correct or not.

Here, we have an example to understand the above validation.

Example

<!DOCTYPE html>  
<html>  
<head>  
    <title>Example of Data format Validation</title>  
    <script>  
        function validateBasic() {  
            var name1 = document.basic.name.value;  
            var cour1 = document.basic.cour1.value;              
            var phone1 = document.basic.phone.value;   
            var id = document.basic.em.value;    
            if (name1 == "") {  
             alert("Enter Your Name"); 
                return false;    
            }  
            if(name1.length<5){ 
                alert("Username should be atleast 5 characters"); 
                return false; 
            } 
            at = id.indexOf("@");     
         dot = id.lastIndexOf(".");     
         if (at < 1 || ( dot - at < 2 )) {        
            alert("Incorrect Email-ID")          
            return false;     
         }      
            if (cour1 == "") {  
             alert("Enter Your Course"); 
                return false;    
            }  
            if (phone1 == "") {  
             alert("Enter Your Phone No."); 
                return false;    
            }  
            if(phone1.length<10 || phone1.length>10){ 
                alert("Mobile number should be of 10 digits"); 
                return false; 
            } 
        }  
 </script>  
</head>  
<body>  
    <center>  
        <h1>Data Format Validation</h1>   
        <form name="basic" action="#" onsubmit="return validateBasic()">  
            <table cellspacing = "2" cellpadding = "2" border = "1"> 
            <tr> 
                <td>Name: </td> 
                <td><input type="text" name="name"></td> 
            </tr> 
                <tr> 
                    <td>Email: </td> 
               <td><input type="email" name="em"></td> 
                </tr> 
            <tr> 
               <td> Course: </td> 
               <td><input type="text" name="cour"></td> 
            </tr> 
            <tr> 
                <td>Phone no.:</td> 
                <td><input type="number" name="phone"></td> 
            </tr> 
        </table> 
        <input type="submit" value="Submit"> 
    </form>  
       <p id="d1"></p>   
    </center>  
</body>  
</html>

Output

After the execution of the above example, we got the following output:

Validation in ES6

If we click on the submit button without entering the data, then we will get the following alert message.

Validation in ES6

Related Topics

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.