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?

2

1 Answer 1

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']);
// ...
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I updated my answer, you are using the method incorrectly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.