不需要的Jquery验证插件数字不起作用 [英] Jquery Validation Plugin digits that aren't required not working

查看:77
本文介绍了不需要的Jquery验证插件数字不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jQuery验证插件

问题是我不希望数字字段为必填字段,我只是想将其验证为数字(如果有人在其中输入了任何内容).

这是我的代码,如果我删除"required:true"部分,那么如果我在其中输入文本并通过表单,该字段将不再引发错误.

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

还有我的HTML

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

在此先感谢您的帮助!

解决方案

这似乎只是input type="number"字段的问题.只要您的字段name与您的规则声明匹配(在这种情况下为phoneNumber ...

),它也才有效.

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

演示: http://jsfiddle.net/L4crh/


但是,作为 http://jsfiddle.net/L4crh/1/


编辑:

据报道,从jQuery Validate版本1.13开始,已解决了type="number"错误.

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

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!

解决方案

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

这篇关于不需要的Jquery验证插件数字不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆