0

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.'
            }
        });

2 Answers 2

2

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

Comments

0

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.

1 Comment

jesus, what was the syntax error? what was the solution? 2000 views and you accepted your answer saying "well i figured it out, go ahead and figure it out yourselves too"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.