仅需整数的数字输入类型? [英] Number input type that takes only integers?

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

问题描述

我正在使用jQuery工具 Validator 来实现HTML5验证jQuery.

I'm using the jQuery Tools Validator which implements HTML5 validations through jQuery.

到目前为止,除了一件事之外,它一直都很棒.在HTML5规范中, input 类型"number"可以同时具有整数和浮点数.

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.

这似乎是非常短视的,因为它仅在数据库字段为带符号的浮点数时才是有用的验证器(对于无符号的int,您将不得不使用pattern验证,从而失去诸如支持它的浏览器的上下箭头.

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 lose extra features like the up and down arrows for browsers that support it).

是否存在另一种 input 类型或 attribute 类型,该类型会限制该输入只是未签名的 integers ?

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正在进行投票.

Ok, guys, I appreciate your time and help, but I see many undeserved up-voting going on :D.

将步长设置为1并不是答案,因为它不会限制输入.您仍然可以在文本框中输入一个负浮点数.

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.

我想知道HTML5是否允许限制数字"类型的输入.到正整数值.对于这个问题,答案似乎是不,不是".

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".

我不想使用模式验证,因为在使用 jQuery 工具验证时,这会导致一些弊端,但是现在看来,该规范不允许采用更简洁的方式来实现此目的.

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.

推荐答案

仅使用HTML即可达到的最佳效果(文档):

The best you can achieve with HTML only (documentation):

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

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

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