HTML5:只接受整数的数字输入类型? [英] HTML5: number input type that takes only integers?

查看:1282
本文介绍了HTML5:只接受整数的数字输入类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery工具 Validator ,它通过jQuery实现HTML5验证。迄今为止,除了一件事外,它一直在努力工作。在HTML5规范中,输入类型number可以同时具有整数和浮点数。这看起来非常短视,因为当数据库字段是带符号的浮点数时它只是一个有用的验证器(对于无符号整数,你必须回退到模式验证,因此松散的额外功能,如向上和向下箭头对于支持它的浏览器)。是否有另一个输入类型或可能将输入限制为无符号整数的属性?我找不到任何,谢谢。

I'm using the jQuery Tools Validator which implements HTML5 validations through jQuery. It's been working great so far except for one thing. In the HTML5 specification, the input type "number" can have both integers and floating point numbers. This seems incredibly short-sighted since it will only be a useful validator when your database fields are signed floating point numbers (for unsigned ints you'll have to fall back to "pattern" validation and thus loose extra features like the up and down arrows for browsers that support it). Is there another input type or perhaps an attribute that would restrict the input to just unsigned integers? I couldn't find any, thanks.

编辑

我感谢你的时间和帮助,但我看到许多不当的投票正在进行:D。将步骤设置为1不是答案,因为它不限制输入。您仍然可以在文本框中输入负浮点数。另外,我知道模式验证(我在原始文章中提到过),但这不是问题的一部分。我想知道HTML5是否允许将number类型的输入限制为正整数值。对于这个问题,答案似乎是不,它不是。我不想使用模式验证,因为这在使用jQuery Tools验证时​​会导致一些缺陷,但现在看来规范并不允许使用更简洁的方式来执行此操作。

Ok guys, I appreciate your time and help, but I see many undeserved up-voting going on :D. Setting the step to 1 is not the answer since it doesn't restrict the input. You can still type a negative floating point number into the textbox. Also, I am aware of pattern validation (I mentioned it in my original post), but that was not part of the question. I wanted to know if HTML5 allowed restricting an input of type "number" to positive integer values. To this question the answer, it seems, would be "no, it does not". I didn't want to use pattern validation because this causes some drawbacks when using jQuery Tools validation, but it now seems that the specification doesn't allow for a cleaner way to do this.

推荐答案

https://www.w3.org/wiki/HTML/Elements/input/number

您可以实现的最佳目标仅限HTML

The best you can achieve with HTML only

    <input type="number" min="0" step="1"/>

这篇关于HTML5:只接受整数的数字输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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