在javascript中验证输入是否介于2个数字之间
我目前正在使用谷歌地图,并试图使用输入验证。我要求用户使用介于0和20之间的数字来设置我位置的缩放 下面是我正在使用的代码。第一个在javascript中验证输入是否介于2个数字之间,javascript,google-maps,validation,Javascript,Google Maps,Validation,我目前正在使用谷歌地图,并试图使用输入验证。我要求用户使用介于0和20之间的数字来设置我位置的缩放 下面是我正在使用的代码。第一个if语句适用于20以上的任何数字,但第二个语句在使用0和-1等数字时不起作用(例如) 有没有解决这个问题的建议 function inputIsValid() { console.log("Inside inputIsValid function"); //Check fo
if
语句适用于20以上的任何数字,但第二个语句在使用0和-1等数字时不起作用(例如)
有没有解决这个问题的建议
function inputIsValid() {
console.log("Inside inputIsValid function");
//Check for Above 20
if (document.getElementById("txtZoom").value > 20) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
//Check for Number below 0
if (document.getElementById("txtZoom").value < 0) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
}
}
}
函数inputIsValid(){
log(“内部inputIsValid函数”);
//检查是否超过20
if(document.getElementById(“txtZoom”).value>20){
警报(“请插入介于0和20之间的数据”);
document.getElementById(“txtZoom”).focus();
返回false;
//检查数字是否低于0
if(document.getElementById(“txtZoom”).value<0){
警报(“请插入介于0和20之间的数据”);
document.getElementById(“txtZoom”).focus();
返回false;
}
}
}
函数inputIsValid(){
$value=document.getElementById(“txtZoom”).value;
如果(($value<0)和($value>20)){
警报(“请输入一个介于0和20之间的值”);
返回false;
}
问题是您将第二个检查嵌套在第一个检查中,因此将永远无法到达该检查。请尝试以下操作:
function inputIsValid() {
var zoomValue = document.getElementById("txtZoom").value;
if (zoomValue > 20 || zoomValue < 0) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
}
}
函数inputIsValid(){
var zoomValue=document.getElementById(“txtZoom”).value;
如果(zoomValue>20 | | zoomValue<0){
警报(“请插入介于0和20之间的数据”);
document.getElementById(“txtZoom”).focus();
返回false;
}
}
gahhhh这样一个新手错误!我猜有时候你只需要第二副眼睛。谢谢!对于这个愚蠢的错误,我很抱歉!会接受答案的。没问题,尤其是在长时间的编码会话之后。我也清理了一点代码,因为原来的代码只是一个复制粘贴。是的,看起来好多了!
function inputIsValid() {
var zoomValue = document.getElementById("txtZoom").value;
if (zoomValue > 20 || zoomValue < 0) {
alert("Please insertAmount between 0 and 20");
document.getElementById("txtZoom").focus();
return false;
}
}