Your Location is: Home > Jquery

Custom Client-Side Validation jQuery adapters don't fire in ASP.Net Core

From: Fidschi View: 3952 wiem 

Question

I'm adding a custom validation attribute to my code and it works perfectly server side. However on client-side the adapter is never called. The addMethod(value, element, params) is called but the value in params is always true but I want to be able to do that var expectedValue = params.expectedvalue;

What am I doing wrong? Thanks in advance

(function($) {
  $.validator.addMethod("rangedecimal", function(value, element, params) {
    var minValue = element.attributes[8].value; // i want to be able to write params.minValue 
    var maxValue = element.attributes[7].value;
    if ((Number(value) > Number(maxValue)) || (Number(value) < Number(minValue))) {
      return false;
    } else {
      return true;
    }
  }, "Le taux nominal  doit être compris entre 0 et 100");

  $.validator.unobtrusive.adapters.add('rangedecimal', ['minValue', 'maxValue'], function(options) {
    // Add validation rule for HTML elements that contain data-testvalidation attribute
    console.log(options, 'options');
    options.rules['rangedecimal'] = {
      // pass the data from data-testvalidation-expectedvalue to
      // the params argument of the testvalidation method
      minValue: options.params['minValue'],
      maxValue: options.params['maxValue']
    };
    // get the error message from data-testvalidation-expectedvalue
    // so that unobtrusive validation can use it when validation rule fails
    options.messages['rangedecimal'] = options.message;
  });
}(jQuery));

Best answer

Remove the DOM ready handler. $(function(){})