Form Validation in JavaScript

Form Validation is usually used on the Server, after the Client has entered all necessary data and then click the Submit button. If the data entered by a Client is incorrect or corrupted, the Server must send all data back to the Client and request that the Form be submitted with the correct information. This is really a long process that puts a lot of burden on the Server.

JavaScript provides a way to verify Form data on a Client's computer before sending it to the Web Server. Form Validation generally performs two functions:

  1. Basic Form Validation - First, the Form must be checked to ensure that all required fields are fully entered. It will require only one loop through each field in the Form and check the data.
  2. Data Format Validation - Second, the entered data must be checked for the accuracy of the Form and the value. Your code must be logically appropriate to check the accuracy of the data.

For example

We will look at the following example to understand the evaluation process. Here is a simple form in HTML format.

 Form Validation  type = "text/javascript" >   action = "webpage.jsp" name = "myForm" onsubmit = " return ( validate ()); " >  cellspacing = "2" cellpadding = "2" border = "1" >  align = "right" > Name  type = "text" name = "Name" />  align = "right" > EMail  type = "text" name = "EMail" />  align = "right" > Zip Code  type = "text" name = "Zip" />  align = "right" > Country  name = "Country" >  value = "-1" selected > [choose yours]  value = "1" > USA  value = "2" > UK  value = "3" > INDIA  align = "right" >  type = "submit" value = "Submit" /> 

Result

Run the above command to see the result

Basic Form Validation

First we see how to implement a Basic Validation. In the above form, we are calling validate () to verify the data when the onsubmit event is taking place. The following code shows the implementation of the validate () function.

  type = "text/javascript" >  

Data Format Validation

We will now see how to verify the form data entered before submitting it to the Web Server.

The following example shows how to verify an email address entered. An email address must contain at least one '@' symbol and one dot (.). In addition, '@' is not the first character of the email address, and the last dot must be at least one character after the '@' symbol.

For example

You try the following code for Email authentication:

  type = "text/javascript" >  

According to Tutorialspoint

Previous article: Handling errors in JavaScript

Next article: Animation in JavaScript

4 ★ | 1 Vote

May be interested

  • What is JavaScript?What is JavaScript?
    javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
  • Object Number in JavaScriptObject Number in JavaScript
    the number object represents the date in the form of a numeric value, or an integer or floating-point number. generally, you don't need to worry about number objects because the browser automatically converts number literals to illustrate the number classes.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • Manage goods with Data Validation in Excel very wellManage goods with Data Validation in Excel very well
    how to manage goods statistics using data validation in excel extremely or - application of the data validation function in excel
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
  • How to Turn on JavaScriptHow to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
  • What is the difference between Java and JavaScript?What is the difference between Java and JavaScript?
    although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages ​​are not technically related to each other.
  • Void keywords in JavaScriptVoid keywords in JavaScript
    void is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.