if (typeof agileui == "undefined") { agileui = {}; } agileui.validator = function(config, sysconfig) { /** * 校验配置 */ this.config = $.extend({}, config ? config : {}); sysconfig = sysconfig ? sysconfig : {}; /** * 校验错误的图标样式 */ var validator_tiperror = sysconfig.errorClass ? sysconfig.errorClass : "validator_tiperror"; /** * 校验通过的图标样式 */ var validator_tipright = sysconfig.passClass ? sysconfig.passClass : "validator_tipright"; var extpkg = agileui.validateExtPkg ? agileui.validateExtPkg : {}; var self = this; /** * 追加校验配置,原子级别为字段级 */ this.append = function(aconfig) { if (!aconfig) { return self; } for ( var i in aconfig) { self.config[i] = (aconfig[i] ? $.extend({}, aconfig[i]) : null); } return self; }; /** * 设置扩展校验包,{'xxx':function(node,e,config){}} */ this.setExtPkg = function(pkg) { extpkg = $.extend(extpkg, pkg ? pkg : {}); return self; }; /** * 校验字段,可独立调用, node dom节点 config 校验配置 form dom节点所在的form "#form" */ this.validateNode = function(node, config, form) { if (!config) { return []; } var v = node.val(); var e = []; if (node.attr("type") && (node.attr("type") == "radio" || node.attr("type") == "checkbox")) { if (config.notNull) { if ($("input[name='" + node.attr("name") + "']:checked", form).length == 0) { e.push(config.notNull.msg ? config.notNull.msg : "请至少选择其中一项"); } } if (config.length) { var l = $("input[name='" + node.attr("name") + "']:checked", form).length; if (config.length.max && l > config.length.max) { e.push(config.length.msg ? config.length.msg : "此项选择数目过多"); } if (config.length.min && l < config.length.min) { e.push(config.length.msg ? config.length.msg : "此项选择数目过少"); } } } else { if (config.notNull) { if (v == null || v == "" || $.trim(v) == "") { e.push(config.notNull.msg ? config.notNull.msg : "此项不可为空"); } } if (v && config.length) { if (!(config.dataType && config.dataType.type && (config.dataType.type == "date" || config.dataType.type == "timestamp"))) { var p = true; var l = v ? self.slength(v) : 0; if (config.length.max) { if (l > config.length.max) { p = false; } } if (config.length.min) { if (l < config.length.min) { p = false; } } if (!p) { e.push(config.length.msg ? config.length.msg : "输入长度不符合要求"); } } } if (v && config.equalWith) { if (v != $("#" + config.equalWith["id"]).val()) { e.push(config.equalWith.msg ? config.equalWith.msg : "输入的值不符"); } } if (v && config.email) { var reg = config.email.regexp ? config.email.regexp : /^\w+((-\w+)|(\.\w+))*\@{1}\w+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig; if (v.search(reg) == -1) { e.push(config.email.msg ? config.email.msg : "输入的邮箱格式不正确"); } } if (v && config.url) { var strRegex = "^((https|http|ftp|rtsp|mms)?://)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + "(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." + "[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; var reg = config.url.regexp ? config.url.regexp : new RegExp(strRegex); if (v.search(reg) == -1) { e.push(config.url.msg ? config.url.msg : "输入的网址格式不正确"); } } if (v && config.regexp) { if (v.search(config.regexp.regexp) == -1) { e.push(config.regexp.msg ? config.regexp.msg : "输入的格式不正确"); } } if (v && config.dataType) { if (config.dataType.type == "date" || config.dataType.type == "timestamp") { var reg = new RegExp( "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"); var regt = new RegExp("^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$"); var ts = v.split(" "); if (ts.length > 2) { e.push(config.dataType.msg ? config.dataType.msg : "输入的日期格式不正确"); } else { if (ts.length == 1) { if (ts[0].search(reg) == -1) { e.push(config.dataType.msg ? config.dataType.msg : "输入的日期格式不正确"); } } else { if (ts[0].search(reg) == -1 || ts[1].search(regt) == -1) { e.push(config.dataType.msg ? config.dataType.msg : "输入的日期格式不正确"); } } } } if (config.dataType.type == "big_decimal") { var reg = new RegExp("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"); if (v.search(reg) == -1) { e.push(config.dataType.msg ? config.dataType.msg : "输入的数字格式不正确"); } } if (config.dataType.type == "long") { var reg = new RegExp("^[0-9]*$"); if (v.search(reg) == -1) { e.push(config.dataType.msg ? config.dataType.msg : "输入的数字格式不正确"); } } } if (v && config.precision && (typeof config.precision.max != "undefined")) { var pnum = v.split("."); if (pnum[0].length > config.precision.max) { e.push(config.precision.msg ? config.precision.msg : "输入的数字整数部分不能超过" + config.precision.max + "位"); } } if (v && config.scale && (typeof config.scale.max != "undefined")) { var pnum = v.split("."); if (pnum.length > 1 && pnum[1].length > config.scale.max) { e.push(config.scale.msg ? config.scale.msg : "输入的数字小数部分不能超过" + config.scale.max + "位"); } } } for ( var c in extpkg) { if (config[c]) { extpkg[c](node, e, config[c]); } } if (config.custom) { config.custom(node, e); } return e; }; this.slength = function(inValue) { inValue = inValue.toString(); if (inValue == null || inValue == "") return 0; var len = 0; for ( var i = 0, l = inValue.length; i < l; i++) { if (inValue.charCodeAt(i) < 128) { len++; continue; } len += 2; } return len; }; /** * 显示校验提示,可独立调用, node dom节点 error 错误数组,如果为空数组则校验无错误 */ this.showTip = function(node, error) { if (!error) { error = []; } var tip = $("#" + node.attr("validator_icon")); tip.data("errors", error); if (error.length != 0) { if (node.attr("validator_errorclass")) { node.addClass(node.attr("validator_errorclass")); } tip.removeClass(validator_tipright); tip.addClass(validator_tiperror); if (node.attr("validator_msg")) { $("#" + node.attr("validator_msg")).html("" + tip.data("errors").join("") + ""); } } else { if (node.attr("validator_errorclass")) { node.removeClass(node.attr("validator_errorclass")); } if (node.attr("validator_msg")) { $("#" + node.attr("validator_msg")).html(""); } if (node.val()) { if ("true" == node.attr("validator_hideicon")) { tip.removeClass(validator_tipright); } else { tip.addClass(validator_tipright); } tip.removeClass(validator_tiperror); } else { tip.removeClass(validator_tipright); tip.removeClass(validator_tiperror); } } if (!tip.data("vrender") && !node.attr("validator_msg")) { tip.data("vrender", "true"); tip.mouseover(function() { if ($("#tippop_" + $(this).attr("id")).length == 0 && tip.data("errors").length != 0) { var pop = $( "