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

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

 1 (function ($) {
 2         $.validator.unobtrusive.parseDynamicContent = function (selector) {
 3             //use the normal unobstrusive.parse method
 4             //$.validator.unobtrusive.parse(selector); changed this line with
 5 
 6             $(selector).find(‘*[data-val = true]‘).each(function () {
 7 
 8                 $.validator.unobtrusive.parseElement(this, false);
 9             });
10 
11             //get the relevant form
12             var form = $(selector).first().closest(‘form‘);
13 
14             //get the collections of unobstrusive validators, and jquery validators
15             //and compare the two
16             var unobtrusiveValidation = form.data(‘unobtrusiveValidation‘);
17             var validator = form.validate();
18 
19             $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
20                 if (validator.settings.rules[elname] == undefined) {
21                     var args = {};
22                     $.extend(args, elrules);
23                     args.messages = unobtrusiveValidation.options.messages[elname];
24                     $(‘[name="‘ + elname + ‘"]‘).rules("add", args);
25                 } else {
26                     $.each(elrules, function (rulename, data) {
27                         if (validator.settings.rules[elname][rulename] == undefined) {
28                             var args = {};
29                             args[rulename] = data;
30                             args.messages = unobtrusiveValidation.options.messages[elname][rulename];
31                             $(‘[name="‘ + elname + ‘"]‘).rules("add", args);
32                         }
33                     });
34                 }
35             });
36         }
37     })($);

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

 

代码添加方法:

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

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