標籤:
本人菜鳥,w3cschool上的東西,覺得很好。
匯入庫
<script src="../js/jquery.js" type="text/javascript"></script><script src="../js/jquery.validate.js" type="text/javascript"></script>
預設的規則,基本上夠用 了,
| 1 |
required:true |
必須輸入的欄位。 |
| 2 |
remote:"check.php" |
使用 ajax 方法調用 check.php 驗證輸入值。 |
| 3 |
email:true |
必須輸入正確格式的電子郵件。 |
| 4 |
url:true |
必須輸入正確格式的網址。 |
| 5 |
date:true |
必須輸入正確格式的日期。日期校正 ie6 出錯,慎用。 |
| 6 |
dateISO:true |
必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22。只驗證格式,不驗證有效性。 |
| 7 |
number:true |
必須輸入合法的數字(負數,小數)。 |
| 8 |
digits:true |
必須輸入整數。 |
| 9 |
creditcard: |
必須輸入合法的信用卡號。 |
| 10 |
equalTo:"#field" |
輸入值必須和 #field 相同。 |
| 11 |
accept: |
輸入擁有合法尾碼名的字串(上傳檔案的尾碼)。 |
| 12 |
maxlength:5 |
輸入長度最多是 5 的字串(漢字算一個字元)。 |
| 13 |
minlength:10 |
輸入長度最小是 10 的字串(漢字算一個字元)。 |
| 14 |
rangelength:[5,10] |
輸入長度必須介於 5 和 10 之間的字串(漢字算一個字元)。 |
| 15 |
range:[5,10] |
輸入值必須介於 5 和 10 之間。 |
| 16 |
max:5 |
輸入值不能大於 5。 |
| 17 |
min:10 |
輸入值不能小於 10。 |
重點是兩種使用方式
1,寫到class中需要引入這個js
<script src="../js/jquery.metadata.js" type="text/javascript"></script>
在html中加入
<input id="password" name="password" type="password" class="{required:true,minlength:5}" />
先寫入
$().ready(function() { $("#signupForm").validate();});
我用的對象的方式,所以name屬性是object.pr 這樣用第二種方式發現不行,他用的是name屬性尋找,應該是。
2,寫到js中
$().ready(function() { $("#signupForm").validate({ rules: { firstname: "required", email: { required: true, email: true }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: "#password" } }, messages: { firstname: "請輸入姓名", email: { required: "請輸入Email地址", email: "請輸入正確的email地址" }, password: { required: "請輸入密碼", minlength: jQuery.format("密碼不能小於{0}個字 符") }, confirm_password: { required: "請輸入確認密碼", minlength: "確認密碼不能小於5個字元", equalTo: "兩次輸入密碼不一致不一致" } } });});messages 處,如果某個控制項沒有 message,將調用預設的資訊<form id="signupForm" method="get" action=""> <p> <label for="firstname">Firstname</label> <input id="firstname" name="firstname" /> </p> <p> <label for="email">E-Mail</label> <input id="email" name="email" /> </p> <p> <label for="password">Password</label> <input id="password" name="password" type="password" /> </p> <p> <label for="confirm_password">確認密碼</label> <input id="confirm_password" name="confirm_password" type="password" /> </p> <p> <input class="submit" type="submit" value="Submit"/> </p></form>
以上為複製內容。更多請訪問http://www.w3cschool.cc/jquery/jquery-plugin-validate.html
網頁中用jquery validate 驗證表單輸入項