.net MVC内置js验证 jquery.validate.unobtrusive.js重置验证操作(备忘,找了很多次了)

时间:2022-06-24 21:26:35
 (function ($) {
$.validator.unobtrusive.parseDynamicContent = function (selector) {
//use the normal unobstrusive.parse method
//$.validator.unobtrusive.parse(selector); changed this line with $(selector).find('*[data-val = true]').each(function () { $.validator.unobtrusive.parseElement(this, false);
}); //get the relevant form
var form = $(selector).first().closest('form'); //get the collections of unobstrusive validators, and jquery validators
//and compare the two
var unobtrusiveValidation = form.data('unobtrusiveValidation');
var validator = form.validate(); $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
if (validator.settings.rules[elname] == undefined) {
var args = {};
$.extend(args, elrules);
args.messages = unobtrusiveValidation.options.messages[elname];
$('[name="' + elname + '"]').rules("add", args);
} else {
$.each(elrules, function (rulename, data) {
if (validator.settings.rules[elname][rulename] == undefined) {
var args = {};
args[rulename] = data;
args.messages = unobtrusiveValidation.options.messages[elname][rulename];
$('[name="' + elname + '"]').rules("add", args);
}
});
}
});
}
})($);

使用场景: 需要动态增加需要验证的表单元素时使用。

代码添加方法:

首先增加代码段,在jquery.validate.unobtrusive.js文件中插入代码段