Search code examples
asp.net-mvc-3jquery-validateunobtrusive-validation

changing the behavior for the unobtrusive validation in asp.net-mvc-3


I want to change the default behavior for the jquery.validate.unobtrusive.js

I want to be able to change options like the error message placement or highlight a field when an error is happening , etc

all that I could do using the jquery.validate plugin alone. Just change some of the validate method options like highlight or errorPlacement

so I want to override some of the functionality of the unobtrusive validation , is it possible without changing the jquery.validate.unobtrusive.js file


Solution

  • You could fetch the native validator from the form data and then subscribe to any standard option. For example errorPlacement:

    $(function() {
        var validator = $('form').data('validator');
        validator.settings.errorPlacement = function(error, element) {
            // do your custom error placement
        };
    
        validator.settings.highlight = function(element, errorClass) {
            // do your custom error highlight
        };
    });