HTML input number step without validation

2019-06-28 08:33发布

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.

1条回答
唯我独甜
2楼-- · 2019-06-28 09:26

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.

查看更多
登录 后发表回答