Jquery - 文本框接受不超过 24 和 60 的数字
Jquery - Textbox accepts numbers not more than 24 & 60
我将用户输入作为时间,所以我在页面上有 2 个文本框。
第一个是小时,第二个是分钟。
我的验证是:-
文本框中只允许输入 2 个数字。
第一个文本框只接受最多 24 个数字
第二个文本框只接受最多 60 个数字
$(".TO").on('keyup keypress blur change', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
} else {
if ($(this).val().length >= parseInt(2) && (e.which != 8 && e.which != 0)) {
return false;
}
}
});
我卡在第二次和第三次验证中了?
尝试使用 max
属性,首先设置为 "24"
input
,第二个设置为 "60"
input
$(".TO").on('keyup keypress blur change', function (e) {
if (+this.value > +this.max) {
this.value = this.max
}
});
jsfiddle http://jsfiddle.net/8tn9h5cu/3/
我将用户输入作为时间,所以我在页面上有 2 个文本框。 第一个是小时,第二个是分钟。
我的验证是:-
文本框中只允许输入 2 个数字。
第一个文本框只接受最多 24 个数字
第二个文本框只接受最多 60 个数字
$(".TO").on('keyup keypress blur change', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
} else {
if ($(this).val().length >= parseInt(2) && (e.which != 8 && e.which != 0)) {
return false;
}
}
});
我卡在第二次和第三次验证中了?
尝试使用 max
属性,首先设置为 "24"
input
,第二个设置为 "60"
input
$(".TO").on('keyup keypress blur change', function (e) {
if (+this.value > +this.max) {
this.value = this.max
}
});
jsfiddle http://jsfiddle.net/8tn9h5cu/3/