javascript - HTML input number step without validation - Stack Overflow

In HTML you can add the step attribute to inputs of type number. I wanted this because I liked how it c

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.

Share Improve this question edited Jan 7, 2017 at 15:31 Michał Perłakowski 92.8k30 gold badges163 silver badges187 bronze badges asked Jan 7, 2017 at 15:19 CutaracaCutaraca 2,5393 gold badges17 silver badges25 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744025706a4545651.html

相关推荐

  • javascript - HTML input number step without validation - Stack Overflow

    In HTML you can add the step attribute to inputs of type number. I wanted this because I liked how it c

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信