vendredi 31 juillet 2015

Double nested if-statement in JavaScript

I'm trying to validate a form with JS. For this, I have an if-statement that checks 1), if email is empty and, 2) if email is valid. To check if the email is valid, I tried two things (both failed):

  1. A nested if-statement, like this:
    if (email == "") {
        return false;
    }else if (email != null) {
        var my_pattern = (regex);
        var my_validate = my_pattern.test(email); 

            if (my_validate == false) {
            return false;
          }
    }

  1. I put the nested if-statement (the one found in the else-if statement in a function that returns true or false; then I put it in the else-if statement, like this:
else if (my-func == false) {

    ...do something
}

If I run any of these if-statements on their own, they work; it's only when I try to nestle them like this, that they get stuck in the nested else-if statement. I want JS to evaluate every nested if-statement and then continue on evaluating the rest of the if-statements.

What can be done to achieve this?

Aucun commentaire:

Enregistrer un commentaire