开发者

if-check in HTML gone awry

开发者 https://www.devze.com 2023-01-25 03:56 出处:网络
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

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:

  1. Use == for comparison, not =.
  2. The empty string is "", not " ". Notice the extra space.

Result:

else if (document.getElementById("user").value == "") {
0

精彩评论

暂无评论...
验证码 换一张
取 消