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

馋奶兔 提交于 2020-02-06 08:49:34

问题


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


回答1:


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).




回答2:


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.




回答3:


This happens because your else if(txtVal.indexOf("%") != -1) is true (so, second if from the top), but condition for "if" inside it is not true (so it doesn't go to "return".




回答4:


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.




回答5:


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;
}


来源:https://stackoverflow.com/questions/8812814/javascript-is-there-a-limit-to-else-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!