我有一个要求输入各种数字的表格
多少个苹果然后输入苹果。身份证是苹果。我尝试过onclick和onfocus,但效果不佳。我只想在他们进入下一个框后进行检查,以确保它是有效数字,或者发出警报并重新关注。
这是我对方法的要求。
function validate (apple) {
var textInput = document.getElementById(apple);
var value = parseInt(textInput.value, 10);
return ( value >= 0 && value <= 99);
alert("number wrong")
}
这是代码
<td><input type = "text" id = "apple" size="1" onkeypress="validate();"/></td></tr>
最佳答案
在所有情况下,您都将从函数返回,然后显示警报。尝试:
function validate (apple) {
var textInput = document.getElementById(apple);
var value = parseInt(textInput.value, 10);
// Store the comparison's return value
var ret = ( value >= 0 && value <= 99);
// Then show the alert if necessary
if (!ret) {
alert("number wrong");
}
// Finally, return the comparison's result
return ret;
}
关于javascript - 表单验证文本框编号为0-99,如果没有警报,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9067422/