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.
Adding Validation to ModelThe validation attributes specify behavior that you want to enforce on the model properties they are applied to. The Required and MinimumLength attributes indicates that a property must have a value; but nothing prevents a user from entering white space to satisfy this validation.
Validation should be in the domain layer, but it can also be used in other layers, like the UI (probably in the Controller or the View in Js) to give fast feedback to the user.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With