Javascript 如何限制html文本字段中数字的最大值和最小值
我有一个文本字段,它应该允许用户输入数字,最大长度应该是2,最大值应该是31,最小值应该是1 我能说出前2个条件,但不知道后2个条件 有人能帮我吗Javascript 如何限制html文本字段中数字的最大值和最小值,javascript,jquery,Javascript,Jquery,我有一个文本字段,它应该允许用户输入数字,最大长度应该是2,最大值应该是31,最小值应该是1 我能说出前2个条件,但不知道后2个条件 有人能帮我吗 <input type="text" name = "ccdate" class="form-control" maxlength="2" onkeypress="return isNumber(event)" > function isNumber(evt) { evt = (evt) ? evt : window.eve
<input type="text" name = "ccdate" class="form-control" maxlength="2" onkeypress="return isNumber(event)" >
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
函数isNumber(evt){
evt=(evt)?evt:window.event;
var charCode=(evt.which)?evt.which:evt.keyCode;
如果(字符码>31&(字符码<48 | |字符码>57)){
返回false;
}
返回true;
}
注意我不想使用HTML5 input type=“number”使用最小和最大属性,如果您想让用户输入数字,则将输入类型作为数字而不是文本
<input type="number" name ="name_u_want_to_give" min="1" max="31" maxlength="2" />
请根据您的要求更正以下代码
`<script type="text/javascript">
function minmax(value, min, max)
{
if(parseInt(value) < 0 || isNaN(value))
return 0;
else if(parseInt(value) > 100)
return 100;
else return value;
}
</script>
<input type="text" name="textWeight" id="txtWeight" maxlength="5" onkeyup="this.value =minmax(this.value, 0, 100)"/>`
`
函数最小值最大值(值、最小值、最大值)
{
if(parseInt(值)<0 | | isNaN(值))
返回0;
如果(parseInt(值)>100),则为else
返回100;
否则返回值;
}
`
试试这个(纯Jquery方法):
HTML:
<input type="text" name = "ccdate" class="form-control" maxlength="2">
<div id="div1"></div>
document.querySelector(“*[name=ccdate]”)。addEventListener(“输入”,函数(){
var num=+this.value,max=31,min=1;//将值转换为数字
如果(!this.value.length)返回false;//允许空字段
this.value=isNaN(num)?min:num>max?max:num
伙计们,由于某些原因,小提琴不起作用,所以我没有在问题中发布抱歉,我不想使用input type=“number”,然后您必须使用javascript限制用户输入不允许的值。从input元素调用函数检查javascript中的条件,如果条件失败,则返回false谢谢回答,正如你所见,我使用纯js来检查用户是否输入了数字或字符串。你的代码完全是jquery。那么将js与jquery混合使用可以吗?你是否可以提供jquery来检查用户是否输入了字符串或数字作为答案,+1.如果可能,请提供纯jsanswer@SpringLearner..i我一定会尝试提供纯js代码今天我有非常糟糕的互联网连接我必须测试纯js答案的代码。。“谢谢你……很高兴能帮助你……”SpringLearner-如果你喜欢短一点的香草JS,请看我的答案。
$('[name="ccdate"]').keyup(function(){
if(parseInt($(this).val()) > 31){
$('#div1').html('value cannot be greater then 31');
$(this).val('');
}
else if(parseInt($(this).val()) < 1)
{
$('#div1').html('value cannot be lower then 1');
$(this).val('');
}
else
{ $('#div1').html(''); }
});
$('[name="ccdate"]').keyup(function(){
if(isNaN($(this).val()))
{
$('#div1').html('entered string');
$(this).val('');
}
else if(parseInt($(this).val()) > 31){
$('#div1').html('value cannot be greater then 31');
$(this).val('');
}
else if(parseInt($(this).val()) < 1)
{
$('#div1').html('value cannot be lower then 0');
$(this).val('');
}
else
{ $('#div1').html(''); }
});
document.getElementById('t1').addEventListener('keyup', function(){
this.value = (parseInt(this.value) < 1 || parseInt(this.value) > 31 || isNaN(this.value)) ? "" : (this.value)
});
document.querySelector("*[name=ccdate]").addEventListener("input", function () {
var num = +this.value, max = 31, min = 1; //converts value to a Number
if(!this.value.length) return false; //allows empty field
this.value = isNaN(num) ? min : num > max ? max : num < min ? min : num;
});