jquery validate添加自定義驗證規(guī)則(驗證郵箱 郵政編碼)
jQuery:validate添加自定義驗證
jQuery.validator.addMethod添加自定義的驗證規(guī)則
addMethod:name, method, message
簡單實例:單個驗證的添加
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>validate.js拓展驗證</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="validate.expand.js"></script>
</head>
<body>
<form action="" method="get" id="tinyphp">
<input type="text" value="" name="isZipCode" />
<input type="submit" value="提交" />
</form>
<script type="text/javascript">
$("#tinyphp").validate({
// 添加驗證規(guī)則
rules: {
isZipCode: { //驗證郵箱
isZipCode: true
}
}
});
</script>
</body>
</html>
validate.expand.js
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的郵政編碼");
添加多個驗證方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>validate.js拓展驗證</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="validate.expand.js"></script>
</head>
<body>
<form action="" method="get" id="tinyphp">
郵編:<input type="text" value="" name="isZipCode" /><br /><br />
名字:<input type="text" value="" name="userName" />
<input type="submit" value="提交" />
</form>
<script type="text/javascript">
$("#tinyphp").validate({
// 添加驗證規(guī)則
rules: {
isZipCode: { //驗證郵箱
isZipCode: true
},
userName:{
required: true,
userName: true,
rangelength: [5,10]
}
},
//重設(shè)提示信息,可省略
messages:{
userName: {
required: "請?zhí)顚懹脩裘?,
rangelength: "用戶名必須在5-10個字符之間"
}
}
});
</script>
</body>
</html>
validate.expand.js
jQuery.validator.addMethod("userName", function(value, element) {
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "用戶名必須在5-10個字符之間");
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的郵政編碼");
- jquery validate.js表單驗證的基本用法入門
- jQuery.Validate驗證庫的使用介紹
- jQuery驗證插件 Validate詳解
- Jquery Validate 正則表達(dá)式實用驗證代碼大全
- jQuery驗證插件validate使用方法詳解
- jQuery Validate 相關(guān)參數(shù)及常用的自定義驗證規(guī)則
- jquery.validate使用攻略 第五步 正則驗證
- jquery.validate 自定義驗證方法及validate相關(guān)參數(shù)
- jQuery Validate讓普通按鈕觸發(fā)表單驗證的方法
- jQuery插件Validate實現(xiàn)自定義表單驗證
- jquery validate 實現(xiàn)動態(tài)增加/刪除驗證規(guī)則操作示例
相關(guān)文章
跟著JQuery API學(xué)Jquery 之三 篩選
跟著JQuery API學(xué)Jquery 之三 篩選,相當(dāng)于條件判斷類型。2010-04-04jquery中dom操作和事件的實例學(xué)習(xí) 下拉框應(yīng)用
jquery中dom操作和事件的實例學(xué)習(xí) 下拉框應(yīng)用實現(xiàn)代碼,需要的朋友可以參考下。2011-12-12jQuery創(chuàng)建自定義的選擇器用以選擇高度大于100的超鏈接實例
這篇文章主要介紹了jQuery創(chuàng)建自定義的選擇器用以選擇高度大于100的超鏈接,實例分析了jQuery自定義選擇器的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03將JavaScript的jQuery庫中表單轉(zhuǎn)化為JSON對象的方法
這篇文章主要介紹了將JavaScript的jQuery庫中表單轉(zhuǎn)化為JSON對象的方法,包括對序列化時空格問題的處理方法,需要的朋友可以參考下2015-11-11Jqgrid設(shè)置全選(選擇)及獲取選擇行的值示例代碼
本篇文章主要介紹了Jqgrid設(shè)置全選(選擇)及獲取選擇行的值示例代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12jquery 實現(xiàn)input輸入什么div圖層顯示什么
這篇文章主要介紹jquery實現(xiàn)input輸入什么div圖層顯示什么的具體過程,需要的朋友可以參考下2014-06-06jquery 實時監(jiān)聽輸入框值變化的完美方法(必看)
下面小編就為大家?guī)硪黄猨query 實時監(jiān)聽輸入框值變化的完美方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01使用jQuery+HttpHandler+xml模擬一個三級聯(lián)動的例子
昨天同學(xué)問我有關(guān)如何快速讀取多層級xml文件的問題,于是我就使用省、市、縣模擬了一個三級聯(lián)動的例子,客戶端使用jQuery實現(xiàn)異步加載服務(wù)器返回的json數(shù)據(jù),服務(wù)器端則使用XPath表達(dá)式查詢數(shù)據(jù)。2011-08-08