Search code examples
javascriptjqueryunobtrusive-validation

How to get unobtrusive validator params for an input?


I have an input with custom unobtrusive validator. There is a number of parameters passed to the validator. Can I access those parameters (outside of my custom validator) using javascript/jQuery and if so how?

Something like:

var validationParams = $('#MyInput').validator.unobtrusive.myvalidator.params;

Solution

  • The answer turned out to be in a place I didn't expect. The unbotrusive validator puts those params in data attributes of the input.

    $('#MyInput').attr('data-val-myvalidator-myparam');
    

    or

    $('#MyInput').data('val-myvalidator-myparam');
    

    Edit: As haim770 pointed out, it's not the unobtrusive validator that puts the params in the data attributes. Those are there from the start (my html is generated with those already). So the unobtrusive validator is actually reading those params from the data attributes.