(function ($) { $.validator.addMethod('dropdownlistrequired', function (value, element, params) { return (value != "" && value != "-1"); }); $.validator.unobtrusive.adapters.add("dropdownlistrequired", function (options) { options.rules["dropdownlistrequired"] = { }; options.messages["dropdownlistrequired"] = options.message; }); $.validator.addMethod('daterequired', function (value, element, params) { return (value != "" && value != "0001-01-01"); }); $.validator.unobtrusive.adapters.add("daterequired", function (options) { options.rules["daterequired"] = { }; options.messages["daterequired"] = options.message; }); $.validator.addMethod('totallength', function (value, element, params) { var totalString = value; $.each(eval('('+params.otherControls+')'), function (i, v) { totalString += $("#" + v).val(); }); if (totalString.length == params.length) { return true; } else { $.each(eval('(' + params.otherControls + ')'), function (i, v) { totalString += $("#" + v).validate(); }); } }); $.validator.unobtrusive.adapters.add("totallength", ["length", "other"], function (options) { options.rules["totallength"] = { length: options.params.length, otherControls: options.params.other }; if (options.message) { options.messages['totallength'] = options.message; } }); $.extend($.fn.validatebox.defaults.rules, { daterequired: { validator: function (value, param) { return (value != "" && value != "0001-01-01"); }, message: 'Please enter at least {0} characters.' } }); $.validator.methods.date = function (value, element) { // Date.parse函数源自另外一个处理日期的库 return this.optional(element) || Date.parse(value) != null; }; jQuery.extend(jQuery.validator.messages, { required: "必须填写", remote: "请修正该字段", email: "请输入正确格式的电子邮件", url: "请输入合法的网址", date: "请输入合法的日期", dateISO: "请输入合法的日期 (ISO).", number: "请输入合法的数字", digits: "只能输入整数", creditcard: "请输入合法的信用卡号", equalTo: "请再次输入相同的值", accept: "请输入拥有合法后缀名的字符串", maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"), minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"), rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"), range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"), max: jQuery.validator.format("请输入一个最大为 {0} 的值"), min: jQuery.validator.format("请输入一个最小为 {0} 的值") }); } (jQuery));