[英]maxlength=5 not working if input type=number
This is frustrating! 这真令人沮丧!
When the input type is text
and I gave maxlength
as 5, it is not allowing me to enter more than 5 characters 当输入类型为
text
且我将maxlength
为5时,不允许输入超过5个字符
<input type="text" maxlength="5" />
If I gave input type as number
and I gave maxlength
as 5, it is allowing more than 5 digits? 如果我将输入类型指定为
number
而将maxlength
为5,则允许输入5个以上的数字吗?
<input type="number" maxlength="5" pattern="[0-9]*" />
Am I missing something? 我想念什么吗?
PS: This is for mobile responsive site! PS:这是针对移动响应网站!
Instead of maxlength
use max
代替
maxlength
使用max
<input type="number" min="1" max="10000" />
Update 更新资料
Small jQuery plugin 小型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));
}
});
};
}($));
try using max
... 尝试使用
max
...
<input type="number" max="99999" />
EDIT : Showing Validation 编辑 :显示验证
<form id='FormValidation'>
<input id="myNum" type="number" min="1" max="99999" />
<input type="text" maxlength="5" />
<input type="submit" />
</form>
Try adding a number greater than 99999 and hitting submit, check the updated fiddle . 尝试添加一个大于99999的数字并点击Submit,检查更新的小提琴 。
This jquery could help too... 这个jQuery也可以帮助...
$('#myNum').keyup( function(e){
var max = $('#myNum').attr('max').length;
if ($(this).val().length >= max) {
$(this).val($(this).val().substr(0, max));
}
});
replace maxlength
with max
用
max
替换maxlength
// Max Length = 5
<input type="number" max="99999" />
// length between 1 to 5
<input type="number" min="1" max="99999" />
Updated Answer. 更新了答案。 It's so simple!
太简单了!
<input type="number" id="nbr"/>
<input type="text" maxlength="5" />
$(document).on('keypress','#nbr', function(e){
if($(this).val().length >= 5)
{
e.preventDefault();
}
});
And you can add a max attribute that will specify the highest possible number that you may insert 并且您可以添加一个max属性,该属性将指定您可以插入的最大数字
<input type="number" max="999" />
if you add both a max and a min value you can specify the range of allowed values: 如果同时添加最大值和最小值,则可以指定允许值的范围:
<input type="number" min="1" max="999" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.