Eatdoku
Eatdoku

Reputation: 6931

jQuery validation plugin , add custom method

I am trying to add a method that validate a field to see if it contains a number value.

so this is what i did, however it is not doing the check for me. anyone has any idea?

thanks

$(document).ready(function() {


        $.validator.addMethod('positiveNumber',
            function(value) {
                return Number(value) > 0;
            }, 'Enter a positive number.');

    });

and

jQuery('form').validate();
jQuery('.validateFieldToCheck').rules('add', {
            positiveNumber:,
            messages: {
                required: 'Field must contain a number.'
            }
        });

Upvotes: 0

Views: 2147

Answers (2)

Eatdoku
Eatdoku

Reputation: 6931

just realized that I have some syntax error, and that was cause some weird behavior of the jQuery validation. Problem solved now once I correct those mistakes.

thanks anyways.

Upvotes: 0

falomir
falomir

Reputation: 1169

AFAIK, you should try, instead of the second block of code you have, the following:

$('#your-form-id').validate({
            rules: {
                yourFormFieldIdToCheck: {
                    required: true,
                    positiveNumber:true  
                }
            },
            messages: {
                yourFormFieldIdToCheck: {
                    required: "This value is required",
                    positiveNumber:"Positive numbers only please"
                }
            }
});

to then verify if is valid as

if ($('#your-form-id').valid() == true) { // Proceed with whatever ...

Upvotes: 2

Related Questions