Search code examples
actionscript-3actionscriptactionscript-2actionscript-1

indexOf Not Working?


I made this so only messages starting with '/msg' or '/logout' will submit.

But, users can still send messages! Is something wrong with my code?

if ((msg.indexOf("/msg") != 0) && (msg.indexOf("/logout") != 0))
{
    return;
}

Solution

  • indexOf will return -1 if the String is not found otherwise it will return the index found which is >= 0

    So your test have to be:

    if ((msg.indexOf("/msg") < 0) && (msg.indexOf("/logout") < 0))
    {
     return;
    }
    

    or

    if ((msg.indexOf("/msg") == -1) && (msg.indexOf("/logout") == -1))
    {
     return;
    }