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

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 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.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 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.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 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.

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.

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.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 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.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 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.

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.

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.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 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.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.