1

I'm trying to setup a "digit" field using the jQuery Validation plugin

The problem is I don't want the digit field to be required, I just want to validate it as digits only, if someone does enter anything into it.

Here is my code, if I remove the "required: true," part, the field no longer throws up an error if I enter text into it and the form gets passed.

$('.js-validate-form').validate({
  rules: {
      phoneNumber: {
      digits: true
    }
  }
});    

And my HTML

<input type="number" name="phone" id="phone" placeholder="Phone (include area code)" value=""/>

Thanks in advance for any help!

1 Answer 1

3

This only seems to be a problem with input type="number" fields. It also only works as long as your field name matches your rule declaration, in this case, phoneNumber...

<input type="text" name="phoneNumber" id="phone" placeholder="Phone (include area code)" value=""/>

DEMO: http://jsfiddle.net/L4crh/


However, there are various phone number rules you can use that are already included in this plugin as part of the additional-methods.js file

DEMO 2: http://jsfiddle.net/L4crh/1/


EDIT:

The type="number" bug has reportedly been resolved as of jQuery Validate version 1.13.

https://github.com/jzaefferer/jquery-validation/releases/tag/1.13.0

Sign up to request clarification or add additional context in comments.

1 Comment

That works perfectly, thanks heaps for your help Sparky! I didn't know that "additional methods" file existed either, will be sure to use that. Thanks again!

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.