I have this simple html:
<input type="text"
placeholder="onlynumbers"
name="onlynumbers"
pattern="\d{1,5}"
maxlength="5">
I need to restrict the number of characters to 5. It works. But I still can type other characters than numbers. I have tried various pattern, for example:
pattern = "[0-9]"
But it is still possible to type other characters than digits.
If I change the type attribute to number, then only digits are accepted in the input field but the maxlength attribute doesn't work. I can enter as many digits as I want. I heard this was a bug in Chrome. Is that the problem?
Is there a cross-browser way to fulfil these requirements:
An input field (text or number, no matters) that only accept digits and doesn't let the user enter more than 5 digits.
patterndoes not prevent the user from entering other characters. To prevent, you need to use JavaScript.max="99999"Also the pattern attribute is implicitly a whole string match. Sopattern="[0-9]"means "only one digit"type='number'ininputelement<input type="number">may be?