因项目需要,扩展了两个jquery-validate插件,一个用于正则表达式验证,一个用于依赖必填验证
//#region jquery validate extend function
jQuery.validator.addMethod("Regexpress", function (value, element, param) {
var msg = $(element).attr("reg-msg");
var result = this.optional(element) || param.test(value);
if (msg)
$.validator.messages.Regexpress = msg;
return result;
}, "");
//<input type="text" id="xxxxx" class="input" validate="{dependence:['aa=1',{on:'bbbbbb', value: '1', equal: false}]}" />
//当on指定的值满足equal value时,应用该验证规则的控件必填,否则显示 当dependence[0]时,该字段必填的消息
jQuery.validator.addMethod("dependence", function (value, element, param) {
var opts = { on: "", value: "", equal: true };
opts = $.extend(param[1]);
if (opts.on) {
if (opts.equal && $("#" + opts.on).val() == opts.value) {
return value.length != 0;
}
else if (!opts.equal && $("#" + opts.on).val() != opts.value) {
return value.length != 0;;
}
else {
return true;
}
}
return true;
});