Skip to main content

JavaScript Form Validation

It is important to validate the form submitted by the user because it can have inappropriate values. So validation is must.
The JavaScript provides you the facility the validate the form on the client side so processing will be fast than server-side validation. So, most of the web developers prefer JavaScript form validation.
Through JavaScript, we can validate name, password, email, date, mobile number etc fields.

JavaScript form validation example

In this example, we are going to validate the name and password. The name can’t be empty and password can’t be less than 6 characters long.
Here, we are validating the form on form submit. The user will not be forwarded to the next page until given values are correct.
  1. <script>  
  2. function validateform(){  
  3. var name=document.myform.name.value;  
  4. var password=document.myform.password.value;  
  5.   
  6. if (name==null || name==""){  
  7.   alert("Name can't be blank");  
  8.   return false;  
  9. }else if(password.length<6){  
  10.   alert("Password must be at least 6 characters long.");  
  11.   return false;  
  12.   }  
  13. }  
  14. </script>  
  15. <body>  
  16. <form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()" >  
  17. Name: <input type="text" name="name"><br/>  
  18. Password: <input type="password" name="password"><br/>  
  19. <input type="submit" value="register">  
  20. </form>  


JavaScript Retype Password Validation

  1. <script type="text/javascript">  
  2. function matchpass(){  
  3. var firstpassword=document.f1.password.value;  
  4. var secondpassword=document.f1.password2.value;  
  5.   
  6. if(firstpassword==secondpassword){  
  7. return true;  
  8. }  
  9. else{  
  10. alert("password must be same!");  
  11. return false;  
  12. }  
  13. }  
  14. </script>  
  15.   
  16. <form name="f1" action="register.jsp" onsubmit="return matchpass()">  
  17. Password:<input type="password" name="password" /><br/>  
  18. Re-enter Password:<input type="password" name="password2"/><br/>  
  19. <input type="submit">  
  20. </form>  


JavaScript Number Validation

Let's validate the textfield for numeric value only. Here, we are using isNaN() function.
  1. <script>  
  2. function validate(){  
  3. var num=document.myform.num.value;  
  4. if (isNaN(num)){  
  5.   document.getElementById("numloc").innerHTML="Enter Numeric value only";  
  6.   return false;  
  7. }else{  
  8.   return true;  
  9.   }  
  10. }  
  11. </script>  
  12. <form name="myform" onsubmit="return validate()" >  
  13. Number: <input type="text" name="num"><span id="numloc"></span><br/>  
  14. <input type="submit" value="submit">  
  15. </form>  


JavaScript validation with image

Let’s see an interactive JavaScript form validation example that displays correct and incorrect image if input is correct or incorrect.
  1. <script>  
  2. function validate(){  
  3. var name=document.f1.name.value;  
  4. var password=document.f1.password.value;  
  5. var status=false;  
  6.   
  7. if(name.length<1){  
  8. document.getElementById("nameloc").innerHTML=  
  9. "<img src='unchecked.gif'/> Please enter your name";  
  10. status=false;  
  11. }else{  
  12. document.getElementById("nameloc").innerHTML="<img src='checked.gif'/>";  
  13. status=true;  
  14. }  
  15. if(password.length<6){  
  16. document.getElementById("passwordloc").innerHTML=  
  17. "<img src='unchecked.gif'/> Password must be at least 6 char long";  
  18. status=false;  
  19. }else{  
  20. document.getElementById("passwordloc").innerHTML="<img src='checked.gif'/>";  
  21. status=true;  
  22. }  
  23. return status;  
  24. }  
  25. </script>  
  26.   
  27. <form name="f1" action="#" onsubmit="return validate()">  
  28. <table>  
  29. <tr><td>Enter Name:</td><td><input type="text" name="name"/>  
  30. <span id="nameloc"></span></td></tr>  
  31. <tr><td>Enter Password:</td><td><input type="text" name="password"/>  
  32. <span id="passwordloc"></span></td></tr>  
  33. <tr><td colspan="2"><input type="submit" value="register"/></td></tr>  
  34. </table>  
  35. </form>  


JavaScript email validation

We can validate the email by the help of JavaScript.
There are many criteria that need to be follow to validate the email id such as:
  • email id must contain the @ and . character
  • There must be at least one character before and after the @.
  • There must be at least two characters after . (dot).
Let's see the simple example to validate the email field.
  1. <script>  
  2. function validateemail()  
  3. {  
  4. var x=document.myform.email.value;  
  5. var atposition=x.indexOf("@");  
  6. var dotposition=x.lastIndexOf(".");  
  7. if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){  
  8.   alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);  
  9.   return false;  
  10.   }  
  11. }  
  12. </script>  
  13. <body>  
  14. <form name="myform"  method="post" action="#" onsubmit="return validateemail();">  
  15. Email: <input type="text" name="email"><br/>  
  16.   
  17. <input type="submit" value="register">  
  18. </form>  

Comments

Popular posts from this blog

Login and Registration Example in JSP with Session

Those who want to start with jsp and MySQL, this is an excellent example for themselves. Here you can learn how to insert data to MySQL using JSP. Also you can learn about session handling in jsp. 1 2 3 4 5 6 7 8 9 10 CREATE TABLE `members` (    `id` int (10) unsigned NOT NULL auto_increment,    `first_name` varchar (45) NOT NULL ,    `last_name` varchar (45) NOT NULL ,    `email` varchar (45) NOT NULL ,    `uname` varchar (45) NOT NULL ,    `pass` varchar (45) NOT NULL ,    `regdate` date NOT NULL ,    PRIMARY KEY   (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; index.jsp 1 2 3 4 5 6 ...

Timer funcitons in JavaScript: Reminder Script

Timers are used in web pages and we will discuss how to setup one and how to use one with an example. The function setTimeout() in function is used and here is its general syntax. mytime = setTimeout(expression, msec); mytime  is the identifier used to identify the current timeout function.  expression  is the statement that is to be executed after the specified time has ticked off.  msec  is the duration of time in milliseconds after which the expression will be executed.  You can see by using setTimeout function we can execute any function or object after a set of time. For example if msec is set 5000 then the expression will be executed after 5 seconds or 5000 milliseconds.  We will try one example where we will have four period buttons and each button will set a different time for another function to execute and display a alert button. We will call it as a reminder script and we will get one alert box based on the period button we click...

Binary Addition

/* File Name : BinAdd.java */    import java.util.*; public class BinAdd    {  public static String addBit(String a, String b, String c)  { String r=""; if(a.equals("1") && b.equals("0") || a.equals("0") && b.equals("1")) { if( c.equals("0")) r="1"; else { r="0"; c="1"; } } else if( a.equals("0") && b.equals("0") ) { if(c.equals("0")) r="0"; else r="1"; } else if( a.equals("1") && b.equals("1") ) { if(c.equals("0")){ r="0"; c="1"; } else { r="1"; c="1"; } } return c+r; }   public static String add(String a, String b)   { String r=""; int len=a.length(); String carry="0"; for(int i=len-1;i...

Real time changing Clock showing date and time

We can display a clock which will be showing the local time of the client computer by using JavaScript. Note that this time displayed is taken from user computer and not from the server.  We have seen in our  date object example how to display current date and time   where the current date and time is displayed once. Here we will try to display changing clock or real time change in date and time by using  setTimeout function . This setTimeout function we have used to trigger the time display function in a refresh rate of 1000 mill seconds ( 1 Sec ). This refresh rate can be changed to any other value. By changing the value to 5000 the clock will refresh and show the exact time once in every 5 seconds.  Here is the demo of this script and code is given below that.  Sat Apr 23 2016 08:27:22 GMT+0530 (IST)   Here is the code <html> <head> <title>(Type a title for your page here)</title> <script type="text/javascript...