Issue
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!
Solution
Example
JS
function limit(element)
{
var max_chars = 2;
if(element.value.length > max_chars) {
element.value = element.value.substr(0, max_chars);
}
}
HTML
<input type="number" onkeydown="limit(this);" onkeyup="limit(this);">
If you are using jQuery you can tidy up the JavaScript a little:
JS
var max_chars = 2;
$('#input').keydown( function(e){
if ($(this).val().length >= max_chars) {
$(this).val($(this).val().substr(0, max_chars));
}
});
$('#input').keyup( function(e){
if ($(this).val().length >= max_chars) {
$(this).val($(this).val().substr(0, max_chars));
}
});
HTML
<input type="number" id="input">
Answered By - will Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.