Jquery Validation Plugin digits that aren't required not working

一笑奈何 提交于 2019-12-17 17:11:04

问题


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:


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



来源:https://stackoverflow.com/questions/20512690/jquery-validation-plugin-digits-that-arent-required-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!