The maxlength
attribute is not working with <input type="number">
. This happens only in Chrome.
<input type="number" class="test_css" maxlength="4" id="flight_number" name="number"/>
The maxlength
attribute is not working with <input type="number">
. This happens only in Chrome.
<input type="number" class="test_css" maxlength="4" id="flight_number" name="number"/>
Chrome (technically, Blink) will not implement maxlength for
<input type="number">
.The HTML5 specification says that maxlength is only applicable to the types text, url, e-mail, search, tel, and password.
The absolute solution that I've recently just tried is:
Change your input type to text and use "oninput" event to call function:
Now use Javascript Regex to filter user input and limit it to numbers only:
Demo: https://codepen.io/aslami/pen/GdPvRY
I have two ways for you do that
First: Use
type="tel"
, it'll work liketype="number"
in mobile, and accept maxlength:Second: Use a little bit of JavaScript:
From MDN's documentation for
<input>
So
maxlength
is ignored on<input type="number">
by design.Depending on your needs, you can use the
min
andmax
attributes as inon suggested in his/her answer (NB: this will only define a constrained range, not the actual character length of the value, though -9999 to 9999 will cover all 0-4 digit numbers), or you can use a regular text input and enforce validation on the field with the newpattern
attribute:You can try this as well for numeric input with length restriction