1

I've been looking for a while to an answer to this question; I'm trying to get the digits setting on jquery validation working without any success. Below are my settings:

{
    min: 0,
    max: 99,
    digits: true,
    messages: {
        min: 'The minimum value is 0',
        max: 'The maximum value is 99',
        digits: 'The value must be a number'
    }
}

I'm assigning my rules dynamically (I realize my code above doesn't list the name of the field I'm validating) and I've verified that all of the other rules (min and max) are validating properly, but when I put alphanumeric characters in the text box the "min" validator fires instead of the digits validator. I'm using the input type of text not number so that should not be the issue. Thanks in advance for any help.

UPDATE:

Also just for anyone searching out there if you are having problems where "min" works but "max" does not you need to make sure that the variable you are assigning to "max" is a JavaScript Number. You can assign a string "0" to min and it will perform the conversion fine but passing a string "99" to max will cause the max validation to stop working. That was another issue I was dealing with for a long time since in this solution I don't know the min and max values at design time. I pull them from attributes in the DOM which come across as strings.

1
  • Please post a fiddle or your validation code here.
    – lshettyl
    Feb 14, 2014 at 14:49

2 Answers 2

3

I'm no expert but what if you put digits: true, above min and max:

{
    digits: true,
    min: 0,
    max: 99,
    messages: {
        min: 'The minimum value is 0',
        max: 'The maximum value is 99',
        digits: 'The value must be a number'
    }
}
4
  • 1
    Actually your suggestion worked; after I put digits first the validation started working. Now, however, if I put a negative number I get the digits error message instead of the min error message. I'm sure that's because "-" is not a digit. Thanks for the help. Feb 14, 2014 at 15:59
  • 1
    Glad I could help! The reason that -1 doesn't validate as you expected is because the digit checking must first convert your input into a string value and check each character in the string to make sure they are all a 0123456789 hence the digits name. If you are looking to validate a number such as -1 or 1.45 then you need number: true, instead of digits: true, because a number needs a more advanced REGEX to make sure of rules such as only one decimal and only one negative symbol are permitted.
    – MonkeyZeus
    Feb 14, 2014 at 16:31
  • If you are looking for some excellent usage examples of jQuery Validate then I suggest checking out the preview for this theme: Metronic Admin Template and look at this specific JS File: form-validation.js
    – MonkeyZeus
    Feb 14, 2014 at 16:33
  • Thanks for all the help and information; i will definitely checkout the link you posted. Feb 15, 2014 at 17:57
0

Add following code before validate

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});

You should use <input type="text" /> only not <input type="number"/>

1
  • I am using a custom success event handler but I can try setting debug to true; also I am using type="text" not type="number". Feb 14, 2014 at 15:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.