×

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side so processing will fasterthan server-side validation. So most of developer prefer client side form validation using JavaScript.

Example

<!DOCTYPE html>  
<html>  
<head>  
<script>  
functionform_validation(){    
var name=document.myform.name.value;    
//var x = document.forms["myform"]["name"].value;  
if (name==null || name==""){    
alert("Name can't be blank");    
return false;    
}    
}  
</script>  
</head>  
<body>  
<form name="myform" method="post" action="register.php" onsubmit="return form_validation()" >  
Name: <input type="text" name="name">  
<input type="submit" value="submit">  
</form>  
</body>  
</html>
Try Now

Another Example of Form validation

<!DOCTYPE html>  
<html>  
<body>  
<form action="/cgi-bin/test.cgi" name="myForm" onsubmit="return(validate());">  
<table cellspacing="2" cellpadding="2" border="1">  
           <tr>  
               <td align="right">Name</td>  
               <td><input type="text" name="Name" /></td>  
           </tr>  
           <tr>  
               <td align="right">Mail id</td>  
               <td><input type="text" name="Mail id" /></td>  
           </tr>       
           <tr>  
               <td align="right">Zip Code</td>  
               <td><input type="text" name="Zip" /></td>  
           </tr>  
           <tr>  
               <td align="right">City</td>  
               <td>  
                  <select name="City">  
                     <option value="-1" selected></option>  
                     <option value="1">New Delhi</option>  
                     <option value="2">Noida</option>  
                     <option value="3">Ghaziabad</option>  
                     <option value="4">Allahabad</option>  
                     <option value="5">Lucknow</option>  
                     <option value="6">Agra</option>  
                     <option value="7">Kanpur</option>  
                     <option value="8">Gurugram</option>  
                     <option value="9">Meerut</option>  
                  </select>  
               </td>  
           </tr>  
           <tr>  
               <td align="right">State</td>  
               <td>  
                  <select name="State">  
                     <option value="-1" selected></option>  
                     <option value="1">Uttar Pradesh</option>  
                     <option value="2">Madhya Pradesh</option>  
                     <option value="3">Bihar</option>  
                     <option value="4">Assam</option>  
                      <option value="5">Uttrakhand</option>  
                     <option value="6">Jharkhand</option>  
                     <option value="7">Gujrat</option>  
                     <option value="8">Haryana</option>  
                     <option value="9">Punjab</option>  
                  </select>  
               </td>  
           </tr>  
           <tr>  
               <td align="right">Country</td>  
               <td>  
                 <select name="Country">  
                     <option value="-1" selected></option>  
                     <option value="1">USA</option>  
                     <option value="2">UK</option>  
                     <option value="3">INDIA</option>  
                   <option value="2">FRANCE</option>  
                     <option value="2">CHINA</option>  
                     <option value="2">JAPAN</option>  
                     <option value="2">RUSSIA</option>  
                  </select>  
               </td>  
           </tr>              
           <tr>  
               <td align="right"></td>  
               <td><input type="submit" value="Submit" /></td>  
           </tr>              
         </table>  
      </form>        
   </body>  
</html>

Try Now


Related Topics

Javascript Re-Password Validation

Example <!DOCTYPE html>   <html>   <head>   <script>   function pass_validation()   {   var firstpassword=document.f1.password1.value;     var secondpassword=document.f1.password2.value;     if(firstpassword==secondpassword){     return true;     }     else{   alert("Password does Not Match");     return false;     }     }    </script>   </head>   <body>   <form name="f1" action="/JavaScript/Index" onsubmit="return pass_validation()">   Password:<input type="password" name="password1" /><br/>   Re-enter :<input type="password" name="password2"/><br/>   <input type="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

JavaScript radio button checked value

What are radio buttons? A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group...

5 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Javascript Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

JavaScript Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 minutes read.