![](/img/trans.png)
[英]“maxlength” is not working with input type=“number” in html textbox
[英]maxlength=5 not working if input type=number
這真令人沮喪!
當輸入類型為text
且我將maxlength
為5時,不允許輸入超過5個字符
<input type="text" maxlength="5" />
如果我將輸入類型指定為number
而將maxlength
為5,則允許輸入5個以上的數字嗎?
<input type="number" maxlength="5" pattern="[0-9]*" />
我想念什么嗎?
PS:這是針對移動響應網站!
代替maxlength
使用max
<input type="number" min="1" max="10000" />
更新資料
小型jQuery插件
(function ($) {
$.fn.maxlength = function (length) {
return this.on('keydown', function () {
var maxlength = length || parseInt($(this).attr('maxlength'), 10) ;
if (maxlength && $(this).val().length >= maxlength) {
$(this).val($(this).val().slice(0, maxlength - 1));
}
});
};
}($));
嘗試使用max
...
<input type="number" max="99999" />
編輯 :顯示驗證
<form id='FormValidation'>
<input id="myNum" type="number" min="1" max="99999" />
<input type="text" maxlength="5" />
<input type="submit" />
</form>
嘗試添加一個大於99999的數字並點擊Submit,檢查更新的小提琴 。
這個jQuery也可以幫助...
$('#myNum').keyup( function(e){
var max = $('#myNum').attr('max').length;
if ($(this).val().length >= max) {
$(this).val($(this).val().substr(0, max));
}
});
用max
替換maxlength
// Max Length = 5
<input type="number" max="99999" />
// length between 1 to 5
<input type="number" min="1" max="99999" />
更新了答案。 太簡單了!
<input type="number" id="nbr"/>
<input type="text" maxlength="5" />
$(document).on('keypress','#nbr', function(e){
if($(this).val().length >= 5)
{
e.preventDefault();
}
});
並且您可以添加一個max屬性,該屬性將指定您可以插入的最大數字
<input type="number" max="999" />
如果同時添加最大值和最小值,則可以指定允許值的范圍:
<input type="number" min="1" max="999" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.