只接受整数的数字输入类型?

问题描述:

我正在使用 jQuery 工具 Validator,它通过 jQuery 实现 HTML5 验证.

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.

这似乎非常短视,因为只有当您的数据库字段是有符号浮点数时,它才会是一个有用的验证器(对于无符号整数,您必须退回到 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).

是否有另一种输入类型或属性可以限制输入为无符号整数?

Is there another input type or perhaps an attribute that would restrict the input to just unsigned integers?

我找不到.

将 step 设置为 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"/>