Tuesday, November 19, 2019

MVC unobstrusive - validate only a single form control

I was trying to find articles detailing how to validate only a single form control using unobstrusive validation, but could not find one. So I digged in a little and found out that the following way works nicely. It may not be the most ideal solution for this problem though, so your suggestions are welcome.



var myForm = $('form');
var validator = myForm.data('validator');
var myValidator = new $.validator(validator.settings, myForm);
myValidator.check($('#myElement')[0]);
myValidator.showErrors();


The key part here is to call check method on a validator object, this method is an internal method so is not available on the default validator object. So I have created a new validator object and passed settings of existing validator objects to it. It is important to pass existing settings since they contain final validation message for each element after combining jquery default message and any custom messages from server side attributes.


No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...