PHP jQuery表单,带验证

Email检测,密码重复检测,可以用于表单发送的各种环境,例如发送反馈,注册帐号

PHP jQuery表单,带验证
XML/HTML Code
  1. <div id="container">  
  2.           
  3.           
  4.         <?if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email']) || !validatePasswords($_POST['pass1'], $_POST['pass2']) || !validateMessage($_POST['message']) ) ):?>  
  5.                 <div id="error">  
  6.                     <ul>  
  7.                         <?if(!validateName($_POST['name'])):?>  
  8.                             <li><strong>Invalid Name:</strong> We want names with more than 3 letters!</li>  
  9.                         <?endif?>  
  10.                         <?if(!validateEmail($_POST['email'])):?>  
  11.                             <li><strong>Invalid E-mail:</strong> Stop cowboy! Type a valid e-mail please :P</li>  
  12.                         <?endif?>  
  13.                         <?if(!validatePasswords($_POST['pass1'], $_POST['pass2'])):?>  
  14.                             <li><strong>Passwords are invalid:</strong> Passwords doesn't match or are invalid!</li>  
  15.                         <?endif?>  
  16.                         <?if(!validateMessage($_POST['message'])):?>  
  17.                             <li><strong>Ivalid message:</strong> Type a message with at least with 10 letters</li>  
  18.                         <?endif?>  
  19.                     </ul>  
  20.                 </div>  
  21.             <?elseif(isset($_POST['send'])):?>  
  22.                 <div id="error" class="valid">  
  23.                     <ul>  
  24.                         <li><strong>Congratulations!</strong> All fields are OK ;)</li>  
  25.                     </ul>  
  26.                 </div>  
  27.         <?endif?>  
  28.   
  29.         <form method="post" id="customForm" action="">  
  30.             <div>  
  31.                 <label for="name">Name</label>  
  32.                 <input id="name" name="name" type="text" />  
  33.                 <span id="nameInfo">What's your name?</span>  
  34.             </div>  
  35.             <div>  
  36.                 <label for="email">E-mail</label>  
  37.                 <input id="email" name="email" type="text" />  
  38.                 <span id="emailInfo">Valid E-mail please, you will need it to log in!</span>  
  39.             </div>  
  40.             <div>  
  41.                 <label for="pass1">Password</label>  
  42.                 <input id="pass1" name="pass1" type="password" />  
  43.                 <span id="pass1Info">At least 5 characters: letters, numbers and '_'</span>  
  44.             </div>  
  45.             <div>  
  46.                 <label for="pass2">Confirm Password</label>  
  47.                 <input id="pass2" name="pass2" type="password" />  
  48.                 <span id="pass2Info">Confirm password</span>  
  49.             </div>  
  50.             <div>  
  51.                 <label for="message">Message</label>  
  52.                 <textarea id="message" name="message" cols="" rows=""></textarea>  
  53.             </div>  
  54.             <div>  
  55.                 <input id="send" name="send" type="submit" value="Send" />  
  56.             </div>  
  57.         </form>  
  58.     </div>  

 validation.php

PHP Code
  1. <?php  
  2.     function validateName($name){  
  3.         //if it's NOT valid  
  4.         if(strlen($name) < 4)  
  5.             return false;  
  6.         //if it's valid  
  7.         else  
  8.             return true;  
  9.     }  
  10.     function validateEmail($email){  
  11.         return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email);  
  12.     }  
  13.     function validatePasswords($pass1, $pass2) {  
  14.         //if DOESN'T MATCH  
  15.         if(strpos($pass1, ' ') !== false) 
  16.             return false; 
  17.         //if are valid 
  18.         return $pass1 == $pass2 && strlen($pass1) > 5; 
  19.     } 
  20.     function validateMessage($message){ 
  21.         //if it's NOT valid  
  22.         if(strlen($message) < 10)  
  23.             return false;  
  24.         //if it's valid  
  25.         else  
  26.             return true;  
  27.     }  
  28. ?>  


原文地址:http://www.freejs.net/article_biaodan_130.html

相关推荐