简体   繁体   中英

Javascript: Is there a limit to “else if” statements?

I am getting value from a text field. I have one if and several else if statement.

The problem the last else if doesn't execute even if the condition is true.

If I change the last else if to if it executes and gives alert. When I change that back to else if the statement doesn't execute. The else if before that is fine as it's firing/executing on a particular condition.

function Valcheck()
{

var txtVal = document.getElementById("sometextField").value;



if(txtVal =="%") 
    {

        alert("% is only allowed with other characters.");
        return;
    }       

    else if(txtVal.indexOf("%") != -1) 
    {
        if((txtVal.indexOf('%')) != (txtVal.length-1)) 
        {
            alert(" % is only allowed at the end.");
            return;
        }       
    } 
    else if(txtVal.indexOf(",") != -1) 
    {
        alert("Comma or comma separated values are not allowed.");
        return;

    } 
    else if(( txtVal.length  >0) &&  (txtVal.indexOf("%") == -1)) 
    {
        alert("Please enter % at the end of the value.");
        return;
    }            

    else if( txtVal.length > 11 ) 
    {

        alert(" Value can't be greater than 11 characters.");
        return;

    }

}

Please help. Thanks

There isn't a limit to the number of if statements. The issue is that one of the previous if statements catches the case you're testing.

Go through each if statements for the case your testing and see if it's beging caught by a previous one.

The problem is that if txtVal.length > 11 , then either this is met:

    else if(txtVal.indexOf("%") != -1)

or this is met:

    else if(( txtVal.length  >0) &&  (txtVal.indexOf("%") == -1))

So that it will never reach the else if( txtVal.length > 11 ) . You need to change this:

    else if(txtVal.indexOf("%") != -1) 
    {
        if((txtVal.indexOf('%')) != (txtVal.length-1)) 
        {
            alert(" % is only allowed at the end.");
            return;
        }       
    }

to this:

    else if(txtVal.indexOf("%") != -1 && txtVal.indexOf('%') != (txtVal.length-1))
    {
        alert(" % is only allowed at the end.");
        return;
    }

so that it doesn't "capture" the case where txtVal.indexOf('%') == (txtVal.length-1) .

发生这种情况的原因是您的else if(txtVal.indexOf("%") != -1)为true(因此,如果从顶部开始,则为第二),但其中的“ if”条件不为true(因此不会继续)返回”。

there is no way that the last 'else-if' be hit: if the textVal has a '%' in it it will go to the second 'else-if' and if it does not have '%' it will go to the one before the last one. so the last if never be hit.

There is no limit to if else nesting. There is a logical barrier to getting to nested if else clauses. Look into using

switch(caseVariable){
  case 1:  document.write("caseVariable = " + 1);
           break;
  case 35:  document.write("caseVariable = " + 35);
           break;
  default: break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM