Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

JS 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. if (name==null || name==””){
  6.   alert(“Name can’t be blank”);
  7.   return false;
  8. }else if(password.length<6){
  9.   alert(“Password must be at least 6 characters long.”);
  10.   return false;
  11.   }
  12. }
  13. </script>
  14. <body>
  15. <form name=“myform” method=“post” action=“abc.jsp” onsubmit=“return validateform()” >
  16. Name: <input type=“text” name=“name”><br/>
  17. Password: <input type=“password” name=“password”><br/>
  18. <input type=“submit” value=“register”>
  19. </form>

Test it Now


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. if(firstpassword==secondpassword){
  6. return true;
  7. }
  8. else{
  9. alert(“password must be same!”);
  10. return false;
  11. }
  12. }
  13. </script>
  14. <form name=“f1” action=“register.jsp” onsubmit=“return matchpass()”>
  15. Password:<input type=“password” name=“password” /><br/>
  16. Re-enter Password:<input type=“password” name=“password2”/><br/>
  17. <input type=“submit”>
  18. </form>

Test it Now


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>

Test it Now


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. if(name.length<1){
  7. document.getElementById(“nameloc”).innerHTML=
  8. ” <img src=’unchecked.gif’/> Please enter your name”;
  9. status=false;
  10. }else{
  11. document.getElementById(“nameloc”).innerHTML=” <img src=’checked.gif’/>”;
  12. status=true;
  13. }
  14. if(password.length<6){
  15. document.getElementById(“passwordloc”).innerHTML=
  16. ” <img src=’unchecked.gif’/> Password must be at least 6 char long”;
  17. status=false;
  18. }else{
  19. document.getElementById(“passwordloc”).innerHTML=” <img src=’checked.gif’/>”;
  20. }
  21. return status;
  22. }
  23. </script>
  24. <form name=“f1” action=“#” onsubmit=“return validate()”>
  25. <table>
  26. <tr><td>Enter Name:</td><td><input type=“text” name=“name”/>
  27. <span id=“nameloc”></span></td></tr>
  28. <tr><td>Enter Password:</td><td><input type=“password” name=“password”/>
  29. <span id=“passwordloc”></span></td></tr>
  30. <tr><td colspan=“2”><input type=“submit” value=“register”/></td></tr>
  31. </table>
  32. </form>

Test it Now

Output:

Enter Name:
Enter Password:

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. <input type=“submit” value=“register”>
  17. </form>

Comments

comments