Rami
Rami

Reputation: 67

Javascript If/Else Issue

I might have it coded a bit wrong for the part I'm asking about but I'll give the solutions that work and the one that doesn't.

First example works:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value == document.UserSignUp.ConfirmEmail.value);
else {alert ("Your email does not match - Please re-enter"); return;}
if (document.UserSignUp.UserPassword.value == document.UserSignUp.ConfirmPassword.value);
else {alert ("Your password does not match - Please re-enter"); return;}}

This second example also works:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
else return true;}

But this last one doesn't work:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
else return true;
if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
{alert ("Your password does not match - Please re-enter"); return false;}
else return true;}

The interesting part about the last example is that the first confirm works but when I add and test out the second confirm, then it doesn't work and I'm not entirely sure why. As a side note, I tested out the second confirm on its own without the first and it worked fine. Has something to do with when a second one or more is added.

Any thoughts or suggestions of what I might be missing?

Upvotes: -1

Views: 95

Answers (6)

Rami
Rami

Reputation: 67

I went with AkshayJ answer because his made more sense. Essentially they said since I had a return in both the first If and first Else, that is why it didn't go to second If and Else. Basically you can have only one return per If & Else if you're gonna have multiple If & Else statements. I can confirm that because I had tried what others said such as taking out the False and True, I had also tried making it look like else {return true;} and else {return;}. So it had nothing to do with whether it had True or False and whether it had { }.

Though the ones saying that its just because of one return in any of the first If and Else or because any If and Else that has a return in the else statement... that's just wrong and a assumption. Its a assumption because of my first example proves that wrong by it showing a return in its Else and the 2nd If & Else worked.

Also when I tested out what AkshayJ said by changing the code to else {} or just simply taking out the Else statement, both ways let the second confirm work.

Plus to continue with two paragraphs/sentences above, a return can work in the If statement as well to let it go on to the second If, so it doesn't just work only in the Else:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
{alert ("Your password does not match - Please re-enter"); return false;}}

The above code works. Thanks for the info AkshayJ and thank you for everyone else that helped as well.

As side note for those that gave their example with a Else If, because of the context of what is in the If & Else, its bad practice to try to change the dynamic flow of how conditions are currently set up. Wasn't asking to combine them when they were already separate. For reference, go to here: w3schools.com/js/js_if_else.asp

So, yes the combine examples worked but its not what I asked about. Nor does it solve the fact that with those combine examples, it doesn't fix the problem if I were to add another If & Else statement. Since AkshayJ explained it, the answer goes to them, thanks again.

Upvotes: 0

Bagi
Bagi

Reputation: 191

You just returned true or false on first if/else check. So when function runs it will return data anyway and never go to password check. Following will work:

function SignUpClick(){
  if (document.UserSignUp.UserEmail.value 
    != document.UserSignUp.ConfirmEmail.value) {
    alert ("Your email does not match - Please re-enter"); return false;
  }
  if (document.UserSignUp.UserPassword.value 
    != document.UserSignUp.ConfirmPassword.value) {
    alert ("Your password does not match - Please re-enter"); return false;
  }
  return true;
}

Upvotes: 1

Ajinkya
Ajinkya

Reputation: 1039

You use return in first if else which break the running code, Try this -

function SignUpClick(){
    if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
    {
        alert ("Your email does not match - Please re-enter"); 
        return false;
    }
    else if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
    {
        alert ("Your password does not match - Please re-enter"); return false;
    }
    else return true;

}

Upvotes: 1

markasoftware
markasoftware

Reputation: 12672

That's because when you do a return, it ends the function right there, and does not continue to the final if/else statement

Upvotes: 1

AkshayJ
AkshayJ

Reputation: 769

This is because you are returning in either of the if else case(1st) so the second if else wont be excuted.. :)

   function SignUpClick(){
    if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
    {alert ("Your email does not match - Please re-enter"); return false;}
    else return true;
//the execution doesnt reach this part of the code given below
    if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
    {alert ("Your password does not match - Please re-enter"); return false;}
    else return true;}

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68433

In your last example, it will not reach the second if condition since you are returning true in the else condition of first if

function SignUpClick()
{
   if (document.UserSignUp.UserEmail.value !=  document.UserSignUp.ConfirmEmail.value ) 
   {  
      alert ("Your email does not match - Please re-enter"); return false;
   }
   else 
      return true; // this line will ensure that second if will not be reached
   if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
   {
      alert ("Your password does not match - Please re-enter"); return false;
   }
   else 
     return true;
}

instead try this

function SignUpClick()
{
   if (document.UserSignUp.UserEmail.value !=  document.UserSignUp.ConfirmEmail.value ) 
   {  
      alert ("Your email does not match - Please re-enter"); 
      return false;
   }
   else if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
   {
      alert ("Your password does not match - Please re-enter"); 
      return false;
   }
   return true;
}

Upvotes: 2

Related Questions