文本框中的最大值和最小值 [英] Maximum and minimum values in a textbox
问题描述
我有一个文本框。有没有一种方法,用户可以输入的最高值是100,最低值是0?
所以如果用户键入的数字超过100,那么它会自动使用keyup()函数将值更改为100,如果用户键入的数字小于0,它将显示为0?我的文本框如下所示:
< input type =textname =textWeightid =txtWeightmaxlength =5/>%< / TD>
这可以使用JavaScript来完成吗?
< script type =>解决方案
这是一个简单的函数, 文本/ JavaScript的 >
函数minmax(value,min,max)
{
if(parseInt(value)< min || isNaN(parseInt(value)))
return 0;
else if(parseInt(value)> max)
return 100;
else返回值;
}
< / script>
< input type =textname =textWeightid =txtWeightmaxlength =5onkeyup =this.value = minmax(this.value,0,100)/>
如果输入不是数字,则用零代替
I have a textbox. Is there a way where the highest value the user can enter is 100 and the lowest is 0?
So if the user types in a number more than 100 then it will automatically change the value to 100 using a keyup() function and if user types in a number less than 0 it will display as 0?
My textbox is below:
<input type="text" name="textWeight" id="txtWeight" maxlength="5"/>%</td>
Can this be done using JavaScript?
Here's a simple function that does what you need:
<script type="text/javascript">
function minmax(value, min, max)
{
if(parseInt(value) < min || isNaN(parseInt(value)))
return 0;
else if(parseInt(value) > max)
return 100;
else return value;
}
</script>
<input type="text" name="textWeight" id="txtWeight" maxlength="5" onkeyup="this.value = minmax(this.value, 0, 100)"/>
If the input is not numeric it replaces it with a zero
这篇关于文本框中的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!