Ajax Form Validation Tutorial

this tutorial will be show how to ajax form validation client side validation in your form using JavaScript. when user typing in the field form will auto check validation without call server side.
for make form validation you need two file php for validation data and javascript, let's go to step 1.

include validate.js

now you can crate formvalidation.php this script will be validation data from your form and will give respone if valid or not.

 

  1. $validationtype = $_GET["validationtype"];
  2. $continueSubmit = true ;
  3.  
  4. switch ($validationtype)
  5. {
  6.         case ‘ajax’:
  7.                 ProcessAjax(); //if validationtype is ajax go to processajax function
  8.                 break;  
  9.         case ‘php’:
  10.                 processPHP();//if it is php call processphp runction
  11.                 break;
  12. }
  13.  
  14. function ProcessAjax()
  15. {
  16.         $required = $_GET["sRequired"];
  17.         $typecheck = $_GET["sTypeCheck"];
  18.         $val = $_GET["val"];
  19.        
  20.  
  21.         validateRequired($required,$val,$typecheck);
  22.                                
  23.  
  24.         switch ($typecheck)
  25.         {
  26.                 case ‘date’:
  27.                         validateDate($val);
  28.                         break;  
  29.                 case ‘email’:
  30.                         validateEmail($val);
  31.                         break;
  32.         }       
  33. }
  34.  
  35.  
  36. function processPHP()
  37. {
  38.         //request  the forms variables
  39.         $user = $_POST["user"];
  40.         $email= $_POST["email"];
  41.         global $continueSubmit;
  42.  
  43.         echo "Username: ";
  44.         validateRequired("required", $user, "none");//validate user
  45.         echo "<br />";
  46.  
  47.         echo "Email: ";
  48.         validateEmail($email) ;//validate email
  49.  
  50.        
  51.         if ($continueSubmit)
  52.         {
  53.                 //submit your form
  54.         }
  55.  
  56. }
  57.  
  58.  
  59. function validateRequired($required,$val,$typecheck)
  60. {
  61.  
  62.  
  63.         if ($required == "required")
  64.         {
  65.                 if ($val == "")
  66.                 {
  67.                        
  68.                         echo "Required";
  69.                         exit();
  70.                 }
  71.                
  72.                 if ($val !== "" &amp;&amp; $typecheck == "none")
  73.                 {
  74.                        
  75.                         echo "Tidak Keren";
  76.                 }
  77.         }
  78.  
  79. }       
  80.  
  81.  
  82. function validateEmail($val)
  83. {
  84. global $continueSubmit ;
  85.        
  86.         if  (ereg ("^([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $val, $regs))
  87.         {
  88.                 echo "Thank You";
  89.         }
  90.         else
  91.         {
  92.                 $continueSubmit = false;
  93.                 echo "Invalid Email Address";
  94.         }       
  95. }

next you create html form give name index.html

 

  1. <form name="form1" id="form1" method="post" action="formvalidation.php?validationtype=php" onSubmit="return validate();">
  2.  
  3. <table width="500">
  4.  
  5.     <tr>
  6.  
  7.         <td width="130">Username </td>
  8.  
  9.         <td width="170"><input type="text" name="user"
  10. tabindex="1" id="user" class="validate required none
  11. usermsg"/></td>
  12.  
  13.         <td id ="usermsg"  class="rules">Required</td>
  14.  
  15.     </tr>
  16.  
  17.  
  18.  
  19.     <tr>
  20.  
  21.         <td>Email </td>
  22.  
  23.         <td><input type="text" name="email" tabindex="2"
  24. id="email" class="validate required email emailmsg" /></td>
  25.  
  26.         <td id="emailmsg" class="rules">Required</td>
  27.  
  28.     </tr>    
  29.  
  30.  
  31.  
  32.     <tr>
  33.  
  34.         <td><input type="submit" name="Submit" value="Submit" tabindex="5" /></td>
  35.  
  36.     </tr>
  37.  
  38.    
  39.  
  40. </table>
  41.  
  42.  
  43.  
  44. </form>
  45.  
  46. </fieldset>

you just need two file to run ajax form validation for see demo and download file you can click here :

Demo Form Validation | Download Form Validation

 

Posted by admin   @   12 October 2009
Tags : , ,

Related Posts

1 Comments

Comments
Oct 16, 2009
11:54 pm
#1 Polprav :

Hello from Russia!
Can I quote a post in your blog with the link to you?

Leave a Comment

Name

Email

Website

Previous Post
« ajax textarea auto-grow
Next Post
Creating Ajax Login Form »
Powered by Wordpress   |   Lunated designed by ZenVerse
Top Footer