码迷,mamicode.com
首页 > 其他好文 > 详细

EasyUI表单验证插件扩展

时间:2020-02-28 17:16:09      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:mob   ++   i++   validator   ide   eth   interface   下划线   求和   

  1 $.extend($.fn.validatebox.defaults.rules, {
  2     regex: {
  3         validator: function (value, param) {
  4             var regex = new RegExp(param[0]);
  5             regex.ignoreCase = true;
  6             regex.global= true;
  7             return regex.test(value);
  8         },
  9         message: ‘请输入正确的信息‘
 10     },
 11     minlength: {
 12         validator: function (value, param) {
 13             return value.length >= param[0];
 14         },
 15         message: ‘长度至少为{0}位.‘
 16     },
 17     IDNumber: {
 18         validator: function (value, param) {
 19             var num = value;
 20             if (num.length != 15 && num.length != 18) {
 21                 return false;
 22             }
 23             var pattern = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|[Xx])?$/; //2个问号是针对15位的 所以这里会导致有一些BUG
 24             if (!pattern.test(num)) return false;
 25             else if (num.length == 18) {
 26                 var arr = new Array(18);
 27                 for (var i = 0; i < num.length; i++) {
 28                     arr[i] = num.charAt(i);
 29                 }
 30                 num = arr;
 31                 var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // 加权因子   
 32                 var valideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // 身份证验证位值.10代表X   
 33                 var sum = 0; // 声明加权求和变量   
 34                 if (num[17].toLowerCase() == ‘x‘) {
 35                     num[17] = 10; // 将最后位为x的验证码替换为10方便后续操作   
 36                 }
 37                 for (var i = 0; i < 17; i++) {
 38                     sum += wi[i] * num[i]; // 加权求和   
 39                 }
 40                 valCodePosition = sum % 11; // 得到验证码所位置   
 41                 return num[17] == valideCode[valCodePosition];
 42             }
 43             else return true;
 44         },
 45         message: ‘请输入正确的身份证号‘
 46     },
 47     faxno: {// 验证传真 
 48         validator: function (value) {
 49             //            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value); 
 50             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);
 51         },
 52         message: ‘传真号码不正确‘
 53     },
 54     passwords: {
 55         validator: function (value, param) {
 56             var old = $(‘#‘ + param).val();
 57             if (value == old) {
 58                 return true;
 59             } else {
 60                 return false;
 61             }
 62         },
 63         message: ‘两次输入的密码不一致‘
 64     },
 65     CHS: {
 66         validator: function (value, param) {
 67             return /^[\u4e00-\u9fa5]+$/.test(value);
 68         },
 69         message: ‘请输入汉字‘
 70     },
 71     ZIP: {
 72         validator: function (value, param) {
 73             return /^[1-9]\d{5}$/.test(value);
 74         },
 75         message: ‘邮政编码不存在‘
 76     },
 77     QQ: {
 78         validator: function (value, param) {
 79             return /^[1-9]\d{4,10}$/.test(value);
 80         },
 81         message: ‘QQ号码不正确‘
 82     },
 83     phone: {// 验证电话号码 
 84         validator: function (value) {
 85             //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);
 86             return /^(\(?\d{2,3}\)?\-?)?[1-9]\d{6,19}(\-?\(?\d{1,4}\)?\-?)?$/i.test(value);
 87         },
 88         //message: ‘格式不正确,请使用下面格式:010-88888888‘
 89         message: ‘格式不正确‘
 90     },
 91     mobile: {
 92         validator: function (value, param) {
 93             //return /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/.test(value);
 94             //return /^1[3|4|5|8][0-9]\d{8}$/.test(value);
 95             return /^1[0-9]{10}$/.test(value);
 96         },
 97         message: ‘手机号码不正确‘
 98     },
 99     phoneormobile: {// 验证电话号码及手机
100         validator: function (value) {
101             return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$|^1[0-9]{10}$/i.test(value);
102         },
103         message: ‘手机或固话格式不正确,固话格式如:010-88888888,手机格式:11位数字‘
104     },
105     loginName: {
106         validator: function (value, param) {
107             return /^[\u4e00-\u9fa5\w_]+$/.test(value);
108         },
109         message: ‘只允许汉字、英文字母、数字及下划线‘
110     },
111     name: {// 验证姓名,可以是中文或英文 
112         validator: function (value) {
113             return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
114         },
115         message: ‘请输入中文‘
116     },
117     unnormal: {// 验证是否包含空格和非法字符 
118         validator: function (value) {
119             return /.+/i.test(value);
120         },
121         message: ‘输入值不能为空和包含其他非法字符‘
122     },
123     safepass: {
124         validator: function (value, param) {
125             return safePassword(value);
126         },
127         message: ‘密码由字母和数字组成,至少6位‘
128     },
129     equalTo: {
130         validator: function (value, param) {
131             return value == $(param[0]).val();
132         },
133         message: ‘两次输入的字符不一至‘
134     },
135     number: {
136         validator: function (value, param) {
137             return /^\d+$/.test(value);
138         },
139         message: ‘请输入数字‘
140     },
141     idcard: {
142         validator: function (value, param) {
143             return idCard(value);
144         },
145         message: ‘请输入正确的身份证号码‘
146     },
147     macNum: {
148         validator: function (value, param) {
149             return /^[a-f\d]{2}-[a-f\d]{2}-[a-f\d]{2}-[a-f\d]{2}-[a-f\d]{2}-[a-f\d]{2}$/.test(value);
150         },
151         message: ‘请输入正确的MacNum‘
152     },
153     integer: {
154         validator: function (value) {
155             return /^[+]?[1-9]+\d*$/i.test(value);
156         },
157         message:"请输入整数"
158     },
159 
160   
161    
162     ageWeiHuoYunShu: {
163         validator: function (value) {
164             var r = /^[+]?[1-9]+\d*$/i.test(value);
165             if (r) {
166                 if (value > 17 && value < 61)
167                     return true;
168                 else
169                     return false;
170             }
171             return false;
172         },
173         message: "范围为18~60"
174     },
175     age: {
176         validator: function (value) {
177             return /^/.test(value);
178         }
179     },
180     // 正数(不包括0)
181     positive: {
182         validator: function (value) {
183             return /^$|^([1-9][0-9]*(\.\d+)?)$|^0\.\d*[1-9]\d*$/.test(value);
184         },
185         message: "请输入大于0的数字"
186     },
187 
188 
189     // 整数或小数
190     custominteger:{
191         validator: function (value) {
192             return /^[0-9]+([.]{1}[0-9]+){0,1}$/.test(value);
193         },
194         message: "请输入正确的数字"
195     },
196     // 正整数(大于0的整数)
197     positiveinteger: {
198         validator: function (value) {
199             return /^[1-9]\d*$/.test(value);
200         },
201         message: "请输入大于0的整数"
202     },
203     // 大于等于0的整数(不能以0开头)
204     zerointeger: {
205         validator: function (value) {
206             return /^(0|[1-9][0-9]*)$/.test(value);
207         },
208         message: "请输入大于或等于0的数字"
209     },
210     bank:{
211         validator: function (value) {
212             return /^[0-9]{16,19}$/.test(value);
213         },
214         message: "请输入正确的银行帐号"
215     },
216     //2015-07-16
217     email: {
218         validator: function (value) {
219             return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value);
220         },
221         message: "电子邮箱格式不正确"
222     },
223     mobileorphone: {
224         validator: function (value) {
225             return /^1[0-9]{10}$|^([0-9]{3,4}-)?[0-9]{7,8}$/.test(value);
226         },
227         message: "请输入正确的手机或座机号码"
228     },
229     ZIPCode: {  //邮编
230         validator: function (value, param) {
231             return /^[1-9]\d{5}$/.test(value);
232         },
233         message: ‘邮政编码不存在‘
234     },
235     // 身份证号码()
236     idcard_Interface: {
237         validator: function (value) {
238             return /^$|^\d{17}(\d|x|X)$/.test(value);
239         },
240         message: "请输入正确的身份证号码"
241     },
242 
243 
244     IDCard: {   //身份证号
245         validator: function (value, param) {
246             var num = value;
247             if (num.length != 15 && num.length != 18) {
248                 return false;
249             }
250             var pattern = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
251             if (!pattern.test(num)) return false;
252             if (num.length == 15) {
253                 var year = num.substring(6, 8);
254                 var month = num.substring(8, 10);
255                 var day = num.substring(10, 12);
256                 var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
257                 if (temp_date.getYear() != parseFloat(year)
258                         || temp_date.getMonth() != parseFloat(month) - 1
259                         || temp_date.getDate() != parseFloat(day)) {
260                     return false;
261                 } else {
262                     return true;
263                 }
264             }
265             else if (num.length == 18) {
266                 var arr = new Array(18);
267                 for (var i = 0; i < num.length; i++) {
268                     arr[i] = num.charAt(i);
269                 }
270                 num = arr;
271                 var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // 加权因子   
272                 var valideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // 身份证验证位值.10代表X   
273                 var sum = 0; // 声明加权求和变量   
274                 if (num[17].toLowerCase() == ‘x‘) {
275                     num[17] = 10; // 将最后位为x的验证码替换为10方便后续操作   
276                 }
277                 for (var i = 0; i < 17; i++) {
278                     sum += wi[i] * num[i]; // 加权求和   
279                 }
280                 valCodePosition = sum % 11; // 得到验证码所位置   
281                 return num[17] == valideCode[valCodePosition];
282             }
283             else return true;
284         },
285         message: ‘请输入正确的身份证号‘
286     },
287  
288     date: {  //日期格式(yyyy-MM-dd、yyyy/MM/dd)
289         validator: function (value, param) {
290             //debugger;
291             var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
292             var r = value.match(reg);
293             if (r == null) return false;
294             var d = new Date(r[1], r[3] - 1, r[4]);
295             return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
296         },
297         message: ‘日期格式错误!格式为:yyyy-MM-dd、yyyy/MM/dd‘
298     },
299     datetime: {  //日期时间格式(yyyy-MM-dd HH:mm:ss、yyyy/MM/dd HH:mm:ss)
300         validator: function (value, param) {
301             var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
302             var r = value.match(reg);
303             if (r == null) return false;
304             var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
305             return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
306         },
307         message: ‘时间格式错误!格式为:yyyy-MM-dd HH:mm:ss、yyyy/MM/dd HH:mm:ss‘
308     },
309 
310     chepaihaoGangAoTai: {
311         validator: function (value, param) {
312             return (/^粤Z{1}[A-Za-z0-9挂]{5}$/.test(value));
313         },
314         message: ‘港澳台内地车牌号格式:粤Zxxxxx(x为字母、数字、或“挂”字)‘
315     },
316     
317 
318     chepaihao: {
319     validator: function (value, param) {
320         return (/^粤[A-Za-z]{1}[A-Za-z0-9挂]{5}$/.test(value));
321     },
322     message: ‘内地车牌号格式:粤yxxxxx(y为字母,x为字母、数字、或“挂”字)‘
323     }
324 
325 });
326 var safePassword = function (value) {
327     return !(/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\‘\"]*)|.{0,5})$|\s/.test(value));
328 }
329 
330 var idCard = function (value) {
331     if (value.length == 18 && 18 != value.length) return false;
332     var number = value.toLowerCase();
333     var d, sum = 0, v = ‘10x98765432‘, w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = ‘11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91‘;
334     var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
335     if (re == null || a.indexOf(re[1]) < 0) return false;
336     if (re[2].length == 9) {
337         number = number.substr(0, 6) + ‘19‘ + number.substr(6);
338         d = [‘19‘ + re[4], re[5], re[6]].join(‘-‘);
339     } else d = [re[9], re[10], re[11]].join(‘-‘);
340     if (!isDateTime.call(d, ‘yyyy-MM-dd‘)) return false;
341     for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i];
342     return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
343 }
344 
345 var isDateTime = function (format, reObj) {
346     format = format || ‘yyyy-MM-dd‘;
347     var input = this, o = {}, d = new Date();
348     var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
349     var len = f1.length, len1 = f3.length;
350     if (len != f2.length || len1 != f4.length) return false;
351     for (var i = 0; i < len1; i++) if (f3[i] != f4[i]) return false;
352     for (var i = 0; i < len; i++) o[f1[i]] = f2[i];
353     o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
354     o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
355     o.dd = s(o.dd, o.d, d.getDate(), 31);
356     o.hh = s(o.hh, o.h, d.getHours(), 24);
357     o.mm = s(o.mm, o.m, d.getMinutes());
358     o.ss = s(o.ss, o.s, d.getSeconds());
359     o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
360     if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false;
361     if (o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
362     d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
363     var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
364     return reVal && reObj ? d : reVal;
365     function s(s1, s2, s3, s4, s5) {
366         s4 = s4 || 60, s5 = s5 || 2;
367         var reVal = s3;
368         if (s1 != undefined && s1 != ‘‘ || !isNaN(s1)) reVal = s1 * 1;
369         if (s2 != undefined && s2 != ‘‘ && !isNaN(s2)) reVal = s2 * 1;
370         return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
371     }
372 }

 

EasyUI表单验证插件扩展

标签:mob   ++   i++   validator   ide   eth   interface   下划线   求和   

原文地址:https://www.cnblogs.com/ZeroSunny/p/12377982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!