Validation是jQuery的插件,提供的方法可以大幅簡化驗證表單的工作,功能也夠一般的需要了。 rules也夠簡單,很容易上手,舉個簡單的例子,用validation來驗證註冊表單。 首先確定驗證的目標是: 1. 必填項不能為空2. 註冊用戶名必須為6-12個字符內3. 合格的email格式4. 密碼必須為6-18個字5. 確認密碼必須跟密碼一致ok,目標很明確了。正片開始 複製程式碼 程式碼如下: <BR>$(function(){ <BR>$( "#regForm" ).validate({ <BR>rules: { <BR> // 註冊使用者名稱<BR>username: { <BR>required: true, <BR>minlength: 5, <BR>maxlength: 12 <BR>}, <BR>// email <BR>email: { <BR>required: true, <BR>email: true <BR>}, <BR>// 密碼<BR>password: { <BR>required: true, <BR>minlength: 6, <BR>maxlength: 18 <BR>}, <BR>// 確認密碼<BR>confirm_password: { <BR>equalTo:"#password" <BR>}, <BR>// 檢定驗證碼<BR>captcha: { <BR>required: true , <BR>remote: "checkCaptcha.php" <BR>} <BR>}, <BR>messages: { <BR>// 註冊使用者名稱<BR>username: { <BR>required: "此項目不能為使用者名稱<BR>username: { <BR>required: "此項目不能為使用者名稱不能為空", <BR>minlength: "不能少於5個字元", <BR>maxlength: "不能多於12個字" <BR>}, <BR>// email <BR>email: { <BR> required: "此項目不能為空", <BR>email: "email格式不正確" <BR>}, <BR>// 密碼<BR>password: { <BR>required: "此項目不能為空" , <BR>minlength: "不能少於6個字", <BR>maxlength: "不能多於18個字元" <BR>}, <BR>// 確認密碼<BR>confirm_password: "兩次輸入密碼不一致", <BR>// 檢定驗證碼<BR>captcha: { <BR>required: "請輸入驗證碼", <BR>remote: "驗證碼輸入錯誤" <BR>} <BR>} <BR>}); <BR>}); 運行後,得到下面的結果更多用法參考 http://www.jb51.net/article/24405.htm