0

I'm trying to make data attributes based validation. However, when I'm adding my numeric method, it's not working.

    $.validator.addMethod('[data-v-numeric="numeric"]', function(value, element) {
        console.log(1);
        return !isNaN(parseInt(value));
    }, objLanguage['validation_numeric']);

What I found in the documentation: "name Type: String The name of the method used to identify it and referencing it; this must be a valid JavaScript identifier"

So I have 2 elements $('[data-v-numeric="numeric"]') and how I thought [data-v-numeric="numeric"] is valid JavaScript selector. But somehow It's not working. Where I made an error then?

Sparky
  • 98,165
  • 25
  • 199
  • 285
LJ Wadowski
  • 6,424
  • 11
  • 43
  • 76

1 Answers1

3

You are using the jQuery.validator.addMethod incorrectly. Please see the documentation http://jqueryvalidation.org/jQuery.validator.addMethod/

var errorMessages = {
    'validation_integer': "Must be integer input.",
    'validation_positive': "Must be positive integer input.",
    // ...
};
$.validator.addMethod('integer', function(value, element) {
        return !isNaN(parseInt(value));
}, errorMessages['validation_integer']);
$.validator.addMethod('positive', function(value, element) {
        return !isNaN(parseInt(value)) && parseInt(value) >= 0;
}, errorMessages['validation_positive']);
// ...
Ke Vin
  • 2,004
  • 1
  • 18
  • 28
  • I need exactly `[data-v-numeric="numeric"]` because I want to have also `[data-v-numeric="integer"]`, `[data-v-numeric="positive"]`, `[data-v-numeric="decimal"]` etc. – LJ Wadowski Aug 30 '14 at 12:11
  • I updated my answer, you are using the method incorrectly. – Ke Vin Aug 30 '14 at 12:28