For <input type="number">
element, maxlength
is not working. How can I restrict the maxlength
for that number element?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
Ugh. It's like someone gave up half way through implementing it and thought no one would notice.
For whatever reason, the answers above don't use the
min
andmax
attributes. This jQuery finishes it up:It would probably also work as a named function "oninput" w/o jQuery if your one of those "jQuery-is-the-devil" types.
As stated by others, min/max is not the same as maxlength because people could still enter a float that would be larger than the maximum string length that you intended. To truly emulate the maxlength attribute, you can do something like this in a pinch (this is equivalent to maxlength="16"):
You can try this as well for numeric input with length restriction
Or if your max value is for example 99 and minimum 0, you can add this to input element (your value will be rewrited by your max value etc.)
Then (if you want), you could check if is input really number
More relevant attributes to use would be
min
andmax
.For who's searching for a global limitation like me:
This catches every keypress on document, and filters it if the input is a "number" input and has a
maxlength
attribute. Then allows the pressed key when the length didn't reach the maxlength. It also works with dynamically added inputs and modals etc.