代码改变世界

Easyui表单验证扩展

2013-08-09 10:45  JerremyZhang  阅读(428)  评论(0编辑  收藏  举报

简介:

使用Easyui,我们省了好多事情,不用为UI费心,只需要关注业务层面即可,下面是一些常用的验证方面的扩展,收藏下自己用

  1 //重载$.fn.validatebox.defaults.rules,实现validator和message即可,value表示元素值,param表示参数,如validtype="length[1,4]"
  2 
  3 $.extend($.fn.validatebox.defaults.rules, {
  4 
  5     minLength: { // 判断最小长度
  6         validator: function (value, param) {
  7             return value.length >= param[0];
  8         },
  9         message: '最少输入 {0} 个字符。'
 10     },
 11     length: {
 12         validator: function (value, param) {
 13             var len = $.trim(value).length;
 14             return len >= param[0] && len <= param[1];
 15         },
 16         message: "内容长度介于{0}和{1}之间."
 17     },
 18     phone: {// 验证电话号码
 19         validator: function (value) {
 20             return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
 21         },
 22         message: '格式不正确,请使用下面格式:020-88888888'
 23     },
 24     mobile: {// 验证手机号码
 25         validator: function (value) {
 26             return /^(13|15|18)\d{9}$/i.test(value);
 27         },
 28         message: '手机号码格式不正确(正确格式如:13450774432)'
 29     },
 30     phoneOrMobile: {//验证手机或电话
 31         validator: function (value) {
 32             return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
 33         },
 34         message: '请填入手机或电话号码,如13688888888或020-8888888'
 35     },
 36     idcard: {// 验证身份证
 37         validator: function (value) {
 38             return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
 39         },
 40         message: '身份证号码格式不正确'
 41     },
 42     floatOrInt: {// 验证是否为小数或整数
 43         validator: function (value) {
 44             return /^(\d{1,3}(,\d\d\d)*(\.\d{1,3}(,\d\d\d)*)?|\d+(\.\d+))?$/i.test(value);
 45         },
 46         message: '请输入数字,并保证格式正确'
 47     },
 48     currency: {// 验证货币
 49         validator: function (value) {
 50             return /^d{0,}(\.\d+)?$/i.test(value);
 51         },
 52         message: '货币格式不正确'
 53     },
 54     qq: {// 验证QQ,从10000开始
 55         validator: function (value) {
 56             return /^[1-9]\d{4,9}$/i.test(value);
 57         },
 58         message: 'QQ号码格式不正确(正确如:453384319)'
 59     },
 60     integer: {// 验证整数
 61         validator: function (value) {
 62             return /^[+]?[1-9]+\d*$/i.test(value);
 63         },
 64         message: '请输入整数'
 65     },
 66     chinese: {// 验证中文
 67         validator: function (value) {
 68             return /^[\u0391-\uFFE5]+$/i.test(value);
 69         },
 70         message: '请输入中文'
 71     },
 72     english: {// 验证英语
 73         validator: function (value) {
 74             return /^[A-Za-z]+$/i.test(value);
 75         },
 76         message: '请输入英文'
 77     },
 78     unnormal: {// 验证是否包含空格和非法字符
 79         validator: function (value) {
 80             return /.+/i.test(value);
 81         },
 82         message: '输入值不能为空和包含其他非法字符'
 83     },
 84     username: {// 验证用户名
 85         validator: function (value) {
 86             return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
 87         },
 88         message: '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
 89     },
 90     faxno: {// 验证传真
 91         validator: function (value) {
 92             //            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
 93             return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
 94         },
 95         message: '传真号码不正确'
 96     },
 97     zip: {// 验证邮政编码
 98         validator: function (value) {
 99             return /^[1-9]\d{5}$/i.test(value);
100         },
101         message: '邮政编码格式不正确'
102     },
103     ip: {// 验证IP地址
104         validator: function (value) {
105             return /d+.d+.d+.d+/i.test(value);
106         },
107         message: 'IP地址格式不正确'
108     },
109     name: {// 验证姓名,可以是中文或英文
110         validator: function (value) {
111             return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
112         },
113         message: '请输入姓名'
114     },
115     carNo: {
116         validator: function (value) {
117             return /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/.test(value);
118         },
119         message: '车牌号码无效(例:粤J12350)'
120     },
121     carenergin: {
122         validator: function (value) {
123             return /^[a-zA-Z0-9]{16}$/.test(value);
124         },
125         message: '发动机型号无效(例:FG6H012345654584)'
126     },
127     email: {
128         validator: function (value) {
129             return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
130         },
131         message: '请输入有效的电子邮件账号(例:abc@126.com)'
132     },
133     msn: {
134         validator: function (value) {
135             return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
136         },
137         message: '请输入有效的msn账号(例:abc@hotnail(msn/live).com)'
138     }, department: {
139         validator: function (value) {
140             return /^[0-9]*$/.test(value);
141         },
142         message: '请输入部门排序号(例:1)'
143     }, same: {
144         validator: function (value, param) {
145             if ($("#" + param[0]).val() != "" && value != "") {
146                 return $("#" + param[0]).val() == value;
147             } else {
148                 return true;
149             }
150         },
151         message: '两次输入的密码不一致!'
152     }
153 });

 

  转自:http://www.jeasyuicn.com/jquery-easyui-validatebox-rules-common-extension.html