一、写法一
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://www.cnblogs.com/0515offer/p/4304792.html
相关资源:Jquery验证框架