閱讀920 返回首頁    go 技術社區[雲棲]


JQuery驗證工具

一、寫法一

var Validator = {

// 郵箱
isEmail : function(s) {
var p = "^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$";
return this.test(s, p);
},

// 手機號碼
isMobile : function(s) {
return this.test(s, /^(180|189|133|134|153|181)\d{8}$/);
},

// 電話號碼
isPhone : function(s) {
return this.test(s, /^[0-9]{3,4}\-[0-9]{7,8}$/);
},

// 郵編
isPostCode : function(s) {
return this.test(s, /^[1-9][0-9]{5}$/);
},

// 數字
isNumber : function(s, d) {
return !isNaN(s.nodeType == 1 ? s.value : s)
&& (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
},

// 判斷是否為空
isEmpty : function(s) {
return !jQuery.isEmptyObject(s);
},

// 正則匹配
test : function(s, p) {
s = s.nodeType == 1 ? s.value : s;
return new RegExp(p).test(s);
}

};

調用形式

if(Validator.isEmail(email)){ ... }


二、寫法二,可以寫成jQuery插件形式

$.Validator = {

isEmail : function(s) {

var p = "^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$";
return this.test(s, p);
},

isMobile : function(s) {
return this.test(s, /^(180|189|133|134|153|181)\d{8}$/);
},

isPhone : function(s) {
return this.test(s, /^[0-9]{3,4}\-[0-9]{7,8}$/);
},

isPostCode : function(s) {
return this.test(s, /^[1-9][0-9]{5}$/);
},

isNumber : function(s, d) {
return !isNaN(s.nodeType == 1 ? s.value : s)
&& (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
},

isEmpty : function(s) {
return !jQuery.isEmptyObject(s);
},

test : function(s, p) {
s = s.nodeType == 1 ? s.value : s;
return new RegExp(p).test(s);
}

};

調用方式

if($.Validator.isEmail(email)){ ... }


原帖地址:https://blog.csdn.net/dyllove98/article/details/8860387


最後更新:2017-04-03 19:06:50

  上一篇:go JQuery驗證工具
  下一篇:go jQuery打印插件JQPRINT