if-check in HTML gone awry
I wrote a short function to do some error checks for a form and am stuck at a portion of code where the final 'elseif' clause in the code below keeps getting executed, even when there is text in the textbox...
could you please advise...thank you..
function errorCheck(){
if(!isInteger(document.getElementById("appleQty").value)){
alert('Please key in an integer in the Apple Quantity text box.');
document.getElementById("appleQty").value="";
document.getElementById("appleQty").focus();
return false;
}
else if(!isInteger(document.getElementById("orangeQty").value)){
alert('Please key in an integer in the Orange Quantity text box.');
document.getElementById("orangeQty").value="";
document.getElementById("orangeQty").focus();
return false;
}
else if(!isInteger(document.getElementById("bananaQty").value)){
alert('Please key in an integer in the Banana Quantity text box.');
document.getElementById("bananaQty").valu开发者_如何学运维e="";
document.getElementById("bananaQty").focus();
return false;
}
else if(document.getElementById("user").value = " "){ /!-Problem, keeps getting repeated-->
document.getElementById("user").focus();
alert('Please key in your name.');
return false;
}
return true;
}
One should use ==
to compare, =
to assign. You are assigning, so add an extra =
to compare.
You are using = as assignment, not equivalence. Use == or ===.
Two problems:
- Use
==
for comparison, not=
. - The empty string is
""
, not" "
. Notice the extra space.
Result:
else if (document.getElementById("user").value == "") {
精彩评论