jQuery EasyUI常用数据验证汇总
(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )
easyui 的validatebox()提供了自定义验证的方法,为此我把一些常用的数据验证汇总了一下,代码如下
$.extend($.fn.validatebox.defaults.rules, { CHS: { validator: function (value, param) { return /^[\u0391-\uFFE5]+$/.test(value); }, message: '请输入汉字' }, ZIP: { validator: function (value, param) { return /^[1-9]\d{5}$/.test(value); }, message: '邮政编码不存在' }, QQ: { validator: function (value, param) { return /^[1-9]\d{4,10}$/.test(value); }, message: 'QQ号码不正确' }, mobile: { validator: function (value, param) { return /^((\(\d{2,3}\))|(\d{3}\-))"]*)|.{0,5})$|\s/.test(value)); } var idCard = function (value) { if (value.length == 18 && 18 != value.length) return false; var number = value.toLowerCase(); 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'; 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])))$/); if (re == null || a.indexOf(re[1]) < 0) return false; if (re[2].length == 9) { number = number.substr(0, 6) + '19' + number.substr(6); d = ['19' + re[4], re[5], re[6]].join('-'); } else d = [re[9], re[10], re[11]].join('-'); if (!isDateTime.call(d, 'yyyy-MM-dd')) return false; for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i]; return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11)); } var isDateTime = function (format, reObj) { format = format || 'yyyy-MM-dd'; var input = this, o = {}, d = new Date(); var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g); var len = f1.length, len1 = f3.length; if (len != f2.length || len1 != f4.length) return false; for (var i = 0; i < len1; i++) if (f3[i] != f4[i]) return false; for (var i = 0; i < len; i++) o[f1[i]] = f2[i]; o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4); o.MM = s(o.MM, o.M, d.getMonth() + 1, 12); o.dd = s(o.dd, o.d, d.getDate(), 31); o.hh = s(o.hh, o.h, d.getHours(), 24); o.mm = s(o.mm, o.m, d.getMinutes()); o.ss = s(o.ss, o.s, d.getSeconds()); o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3); if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false; if (o.yyyy < 100) o.yyyy += (o.yyyy > 30 "htmlcode"><table class="grid" id="uiform"> <tr><td>登录名:</td><td><input required="true" id="txtUsername" type="text" class="txt03" /></td><td>真实姓名:</td><td><input id="txtTruename" validType="CHS" required="true" type="text" class="txt03" /></td></tr> <tr><td>登录密码:</td><td><input validType="safepass" required="true" id="txtPassword" name="password" type="password" class="txt03" /></td><td>Email:</td><td><input id="txtEmail" name="email" validType="email" type="text" class="txt03" /></td></tr> <tr><td>身份证号:</td><td><input validType="idcard" id="txtIdcard" name="idcard" type="text" class="txt03" /></td><td>QQ:</td><td><input validType="QQ" id="txtQq" name="qq" type="text" class="txt03" /></td></tr> <tr><td>手机:</td><td><input validType="mobile" id="txtMobile" name="mobile" type="text" class="txt03" /></td><td>电话:</td><td><input id="txtTel" name="tel" type="text" class="txt03" /></td></tr> <tr><td>家庭住址:</td><td colspan="3"><input validType="equalTo[txtMobile]" style="width:80%" id="txtHomeaddr" name="homeaddr" type="text" class="txt03" /></td></tr> <tr><td>备注:</td><td colspan="3"> <input type="text" style="width:80%" class="txt03" id="txtRemark"></textarea></td></tr> <tr><td> </td><td colspan="3"><input id="Checkbox1" type="checkbox" /><label>超级管理员</label> <input id="Checkbox2" type="checkbox" /><label>禁用</label></td></tr> </table>这一段JS 是必不可少的
$(function(){ $('#uiform input').each(function () { if ($(this).attr('required') || $(this).attr('validType')) $(this).validatebox(); }) });这样就ok了
如果在提交时验证表单有没有通过验证,则可使用下面的代码
var flag = true; $('#uiform input').each(function () { if ($(this).attr('required') || $(this).attr('validType')) { if (!$(this).validatebox('isValid')) { flag = false; return; } } }) if (flag) alert('验证通过!'); else alert('验证失败!');以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:jQuery EasyUI封装简化操作