简体   繁体   English

jQuery:如何仅在Keydown事件上验证HTML5输入类型编号的键输入浮点值

[英]JQuery: How to validate key input float value only for HTML5 input type number on Keydown Event

I am validating the form with HTML5 tag, input type number and I want to keep key validating for float value only when the user input the value in box. 我正在使用HTML5标签,输入类型编号来验证表单,并且仅在用户在框中输入值时才想对浮点值进行键验证。 But I cannot get the correct value since I design the form with input type number and I input the value "12." 但是我无法获得正确的值,因为我使用输入类型编号设计了表单并且输入了值“ 12”。 I will get the value as "12" and if I type "12.." I will get value as empty string(""), actually the input should not allow to type "12..". 我将获得的值为“ 12”,如果我键入“ 12 ..”,我将获得的值为空字符串(“”),实际上输入不应允许键入“ 12 ..”。 So is there any possible way to make the key press validation for this? 那么,有没有办法对此进行按键验证?

In HTML5, the number type has a step value, max and min proprieties that validate it when submitted. 在HTML5中,数字类型具有步长值,最大和最小属性,可在提交时对其进行验证。 The setp value also used by implementations for the stepper buttons (ie pressing up increases by step). setp值也被实现用于步进按钮(即,按逐步增加)。

Step can be a float: 步骤可以是浮点数:

<input type="number" step="0.01">

For additional info read this blog post: http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/ 有关其他信息,请阅读此博客文章: http : //blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/

But if you want to do it with jQuery you have to implement it yourself, by capturing the event and checking the entered data against your rules. 但是,如果要使用jQuery,则必须自己捕获捕获事件并根据规则检查输入的数据,以自己实现。

NB : As @artm commented, except for IE 8/9 and Opera Mini, this HTML5 feature is present across the rest of major browsers. 注意 :正如@a​​rtm所评论的那样,除了IE 8/9和Opera Mini外,其余主要浏览器都具有此HTML5功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM