//设置参数 this.setOptions = function (options) { for (var key in options) { if (key in this.options) { this.options[key] = options[key]; } } };
//检测表单 包括是否为空,最大值 最小值,正则验证 this.checkForm = function () { $("#formValidate").submit(function () { var formChind = $("#formValidate").children(); var testResult = true; formChind.each(function (i) { var child = formChind.eq(i); var value = child.val(); var len = value.length; var childSpan = child.next();
//属性中是否为空的情况 if (child.attr("empty")) { if (child.attr("empty") == "yes" && value == "") { if (childSpan) { childSpan.html(""); } return; } }
//属性中min 和 max 最大和最小长度 var min = null; var max = null; if (child.attr("min")) min = child.attr("min"); if (child.attr("max")) max = child.attr("max"); if (min && max) { if (len < min || len > max) { if (childSpan) { childSpan.html(""); childSpan.html(" 字符串长度在" + min + "与" + max + "之间"); testResult = false; return; } } } else if (min) { if (len < min) { if (childSpan) { childSpan.html(""); childSpan.html(" 字符串长度应大于" + min); testResult = false; return; } } } else if (max) { if (len > max) { if (childSpan) { childSpan.html(""); childSpan.html(" 字符串长度应小于" + max); testResult = false; return; } } }
//正则校验 if (child.attr("validate")) { var type = child.attr("validate"); var result = _this.check(value, type); if (childSpan) { childSpan.html(""); if (result != true) { childSpan.html(" " + result); testResult = false; } } }
}); return testResult; }); };
//检测单个正则选项 this.check = function (value, type) { if (this.options[type]) { var val = this.options[type]["reg"]; if (!val.test(value)) { return this.options[type]["str"]; } return true; } else { return "找不到该表单验证正则项"; } };