Desmond
Desmond

Reputation: 1736

Jquery Validation Plugin digits that aren't required not working

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!

Upvotes: 1

Views: 2042

Answers (1)

Sparky
Sparky

Reputation: 98728

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

Upvotes: 3

Related Questions