admin 管理员组文章数量: 1086019
In HTML you can add the step
attribute to inputs of type number. I wanted this because I liked how it can simplify the input for the user, by only pressing the up/down key.
I found out that an additional feature of this attribute is, that it forces the user to stay in that interval. HTML does this with validation. You can enter any value manually, but it won't let you submit the form. I don't want this!
To work around with this, I found that there is a novalidate
attribute that is used in the <form>
tag, which works great. But sadly, the browser support is poor.
I guess I'd need to remove the step
attribute and write the functionality by myself with JavaScript/jQuery by triggering the keydown event on it.
In HTML you can add the step
attribute to inputs of type number. I wanted this because I liked how it can simplify the input for the user, by only pressing the up/down key.
I found out that an additional feature of this attribute is, that it forces the user to stay in that interval. HTML does this with validation. You can enter any value manually, but it won't let you submit the form. I don't want this!
To work around with this, I found that there is a novalidate
attribute that is used in the <form>
tag, which works great. But sadly, the browser support is poor.
I guess I'd need to remove the step
attribute and write the functionality by myself with JavaScript/jQuery by triggering the keydown event on it.
-
2
I think I have the same question / problem. This did not work for me:
<input type="number" name="futureDailyAdSpend" value="138.03" step="50" min="50" formnovalidate/>
. I get this error: "Please enter a valid value. The two nearest valid values are 100 and 150." I want to allow users to click the up/down arrows to step in increments of 50, but I also want to allow users to type in a more precise number. – Ryan Commented Aug 7, 2017 at 16:55
2 Answers
Reset to default 3But sadly, the browser support is poor.
That's not true. According to MDN, IE supports the novalidate
attribute since version 10, and input[type=number]
also since version 10. This means that if a browser supports number inputs, it most likely also supports the novalidate
attribute.
This is a React implementation but there is a way to achieve this. Essentially:
- Set
step
as"any"
(effectively disables the browser restriction on the value, no validation applies, BUT the stepper still appears) - Control the input value from JS
- Track stepper/arrow changes by using
inputType
to differentiate between typing/pasting input vs stepper/arrow input - Detect stepper/arrow events via mouse events, manually apply the change to the input value
See the working example here.
本文标签: javascriptHTML input number step without validationStack Overflow
版权声明:本文标题:javascript - HTML input number step without validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744025707a2520593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论