开发者

Adding validation with MVC 3 & jQuery Validator in execution time

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

<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.

Have a way to add validation in this field 开发者_如何学运维using jQuery?

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

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

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


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


I solved here using

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

parseDynamicContent I got here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜