It seems that neither of the "maxlength", "min" or "max" HTML attributes have the desired effect on iPhone for the following markup:
<input type="number" maxlength="2" min="0" max="99"/>
Instead of limiting the number of digits or the value of the number entered, the number is just left as it was typed in on iPhone 4. This markup works on most other phones we tested.
What gives?
Any workarounds?
If it is important to the solution, we use jQuery mobile.
Thanks!
as Andrew said you need to change the type to range, beside that you are going to have problems with IE and FF and probably old ipad browser because that's from HTML 5, and it's a "Kind New". Take a look to this person who tried the range with JS, Specify range of numbers to input in number field using javascript
Here a more optimized jQuery version that caches the selectors. It also uses the maxlength attribute that is not supported by input type number.
Check this out, it works for me;
Is there a minlength validation attribute in HTML5?
You can use JavaScript to check how many characters are in the box and if there are too many, remove one: http://www.webcodingtech.com/javascript/limit-input-text.php
Example
JS
HTML
If you are using jQuery you can tidy up the JavaScript a little:
JS
HTML
I know this question is one year old, but I don't prefer the selected answer. Because the way it works is that it shows the character that is typed and then removes it.
In contrast I will propose another way which is similar to Will's, it is just better in the way that it prevents the character from being shown. Also I've added an
else if
block to check if the character is a number and then it is a good function to validate the number fields.HTML
JS