I am using jQuery validator to validate my fields on a form in a Razor view that includes Kendo textboxes. The validations are set up as data annotations on the models. Everything is working perfectly for validations such as max length, email type, etc. However, I cannot get this to run for required fields until the form is submitted. All the information I have read seems to indicate that this is working as designed. Well, I don't want users to have to do a post-back just to find out that they left out one required field. Isn't there any way to include the data-val-required in the jQuery validator when the user tabs off the text box?
I have tried adding the following to my document ready function, but I can't seem to force a validation to occur on the element:
$('#myForm').validate({
onfocusout: function (element) {
var required = $(element).attr('data-val-required');
if (required) {
$(element).attr('required', 'required');
//alert('Element ' + $(element).attr('id') + ' ' + $(element).attr('required'));
$(element).validate();
}
});
At this point, I'll even accept a full-scale validation on the entire form when the page loads. I'm much more used to Angular and Breeze that include ALL validations in there, so I am hoping that I just missed an obvious fix for this problem.
The previous question that sort of addressed this subject was for re-editing an input. The solution posed there will not work because I want a validation prior to input being entered--just on the onblur event--or a specific call to validation when the user clicks the submit button before the controller method is called. I am beginning to think this is impossible and really wish we were using something other than Razor. Is there something else that can validate forms easily?
Thanks for all your help!