在执行时使用MVC 3和jQuery验证器添加验证

时间:2022-08-24 22:16:47

I have a form with validation rendered by c# when the page is loaded, the rendered fields like so:

我有一个表单,在加载页面时由c#呈现验证,渲染的字段如下:

<input autocomplete="off" class="input-validation-error" data-val="true" data-val-number="The field Idade must be a number." data-val-range="message here" data-val-range-max="25" data-val-range-min="16" data-val-required="The Idade field is required." id="Content_MyFieldId" maxlength="3" name="Content.MyFieldId" value="0" type="text">

and I'm trying put a new html object equals the example with jQuery, but this new field is not validated when I submit the form.

我正在尝试将一个新的html对象等同于jQuery的示例,但是当我提交表单时,这个新字段未经过验证。

Have a way to add validation in this field using jQuery?

有办法使用jQuery在此字段中添加验证吗?

PS: I don't want to use manual method like so:

PS:我不想使用这样的手动方法:

$("#field").rules("add", {
    required: true,
    messages: {
        required: "Required input"
    }
});

Because I have the rules in the input field, I only want to apply it.

因为我在输入字段中有规则,所以我只想应用它。

2 个解决方案

#1


23  

Feels like a bit of a hack, but here's how I've done it.

感觉有点像黑客,但这就是我做的。

// Target Form
var $form = $("**form selector**");

// Unbind existing validation
$form.unbind();
$form.data("validator", null);

// Check document for changes
$.validator.unobtrusive.parse(document);

// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);

Rich

丰富

#2


6  

I solved here using

我用这里解决了

jQuery.validator.unobtrusive.parseElement($("#element")[0], false);
$.validator.unobtrusive.parseDynamicContent($("#element")[0]);

parseDynamicContent I got here

parseDynamicContent我来到这里

#1


23  

Feels like a bit of a hack, but here's how I've done it.

感觉有点像黑客,但这就是我做的。

// Target Form
var $form = $("**form selector**");

// Unbind existing validation
$form.unbind();
$form.data("validator", null);

// Check document for changes
$.validator.unobtrusive.parse(document);

// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);

Rich

丰富

#2


6  

I solved here using

我用这里解决了

jQuery.validator.unobtrusive.parseElement($("#element")[0], false);
$.validator.unobtrusive.parseDynamicContent($("#element")[0]);

parseDynamicContent I got here

parseDynamicContent我来到这里