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:

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...