使用jQuery validate 驗證註冊表單一實例示範_jquery

來源:互聯網
上載者:User

Validation是jQuery的外掛程式,提供的方法可以大大簡化驗證表單的工作,功能也足夠一般的需要了。rules也足夠簡單,很容易上手,舉個簡單的例子,用validation來驗證註冊表單。

首先確定驗證的目標是:
1. 必填項不可為空
2. 註冊使用者名稱必須為6-12個字元內
3. 合格的email格式
4. 密碼必須為6-18個字元
5. 確認密碼必須跟密碼一致

ok,目標很明確了。正片開始

複製代碼 代碼如下:

<script type="text/javascript" src="../lib/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../lib/jquery.validate.min.js"></script>
<script type="text/javascript">
$(function(){
$( "#regForm" ).validate({
rules: {
// 註冊使用者名稱
username: {
required: true,
minlength: 5,
maxlength: 12
},
// email
email: {
required: true,
email: true
},
// 密碼
password: {
required: true,
minlength: 6,
maxlength: 18
},
// 確認密碼
confirm_password: {
equalTo:"#password"
},
// 檢驗驗證碼
captcha: {
required: true,
remote: "checkCaptcha.php"
}
},
messages: {
// 註冊使用者名稱
username: {
required: "此項不可為空",
minlength: "不能少於5個字元",
maxlength: "不能多於12個字元"
},
// email
email: {
required: "此項不可為空",
email: "email格式不正確"
},
// 密碼
password: {
required: "此項不可為空",
minlength: "不能少於6個字元",
maxlength: "不能多於18個字元"
},
// 確認密碼
confirm_password: "兩次輸入密碼不一致",
// 檢驗驗證碼
captcha: {
required: "請輸入驗證碼",
remote: "驗證碼輸入錯誤"
}
}
});
});
</script>

運行後,得到下面的結果


更多用法參考 http://www.jb51.net/article/24405.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.