Jump to content

JavaScript else if statement not working right.


Err

Recommended Posts

I'm stumped. I can't get this JavaScript to work right. Based on the below code, what it's supposed to do is: if the option selected in a drop down box is the 4th one, then, if card number (cnumber) is not equal to 13 or 16 digits then display an alert telling them this. Anyways, it is doing the opposite, when I put in 13 or 16 digits it gives me the alert. This code is part a huge chunk of code, which is why there's an else if in the beginning. I don't get any scripting errors in Firefox or IE. Also, if I put a == false after 16 and change the NOT equal to signs to equal to, like value.length == 16) == false) then when I enter 16 digits, the script completely falls through and submits this form, no alerts, moreover when I enter 13 digits I still get an alert. Any suggestions?

else if (getid.ctype.selectedIndex == 4) {  if ((getid.cnumber.value.length != 13) || (getid.cnumber.value.length != 16)) {	alert("Number lengh for the Visa card does not match 13 or 16 digts.");	cnumber.focus();	return false;  }}

Edit: I will make an example page if anyone requests it. I just want people to check my code first since it's the source of my problem.

Link to comment
Share on other sites

Yes. Here's my entire if statement, problem script where it is bold.

    if (getid.plan.selectedIndex == 0) {      alert("Please select your hosting plan.");      plan.focus();      return false;    }    else if ((getid.plan.selectedIndex == 2) && (getid.domain.value == "")) {      alert("Please enter in a domain name.");      document.getElementById("domain").style.display = "block";      domain.focus();      return false;    }    else if ((getid.plan.selectedIndex == 3) && (getid.domain.value == "")) {      alert("Please enter in a domain name.");      document.getElementById("domain").style.display = "block";      domain.focus();      return false;    }    else if ((getid.plan.selectedIndex == 2) && (getid.ext.selectedIndex == 0)) {      alert("Please select a domain extension.");      ext.focus();      return false;    }    else if ((getid.plan.selectedIndex == 3) && (getid.ext.selectedIndex == 0)) {      alert("Please select a domain extension.");      ext.focus();      return false;    }    else if (getid.ctype.selectedIndex == 0) {      alert("Please select a card type.");      ctype.focus();      return false;    }    else if (getid.cnumber.value == "") {      alert("Please enter in the card number.");      cnumber.focus();      return false;    }    else if (chkNumeric(getid.cnumber.value) == false) {      alert("Only numbers are allowed for the card number. No spaces, dashes or periods.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 1) && (getid.cnumber.value.length != 16)) {      alert("Number lengh for the Discover card does not match 16 digits.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 2) && (getid.cnumber.value.length != 15)) {      alert("Number length for the American Express card does not match 15 digits.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 3) && (getid.cnumber.value.length != 16)) {      alert("Number lengh for the Master Card does not match 16 digits.");      cnumber.focus();      return false;    }[b]    else if (getid.ctype.selectedIndex == 4) {      if ((getid.cnumber.value.length != 13) || (getid.cnumber.value.length != 16)) {        alert("Number lengh for the Visa card does not match 13 or 16 digts.");        cnumber.focus();        return false;      }    }[/b]
Link to comment
Share on other sites

The problem is that you are using an OR operator, so it will alert if the length is not 13 OR the length is not 16. And since the length cannot be 13 and 16 at once, it will always be true. Change it to AND, or use NOT

  if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) {	alert("Number lengh for the Visa card does not match 13 or 16 digts.");	cnumber.focus();	return false;  }

Link to comment
Share on other sites

I tried the code, but this time when I typed in either 13 or 16 the script would fall through and the form submits (not suppose to yet), however it would give alerts when it wasn't 13 or 16, so we're getting closer. I thought that's what the OR operator was for, to check if either value is true or in this case, not true. Doesn't the AND check if both are true at the same time?I'm seeing it as, if not 13 OR 16 then do this. if it isn't either do this.if not 13 AND 16 then do this. if it isn't both do this.

Link to comment
Share on other sites

Edit: I went and took a break from this and came back. I found out why it would fall through.

 else if (getid.ctype.selectedIndex == 4) {if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) {alert("Number lengh for the Visa card does not match 13 or 16 digts.");cnumber.focus();return false;}}

The nested IF is saying to return false if not 13 and 16, once it's out of that IF, it jumps back to the ELSE IF, it sees nothing and returns TRUE, and that's causing it to submit my form. My next question is, how can I make the ELSE IF go back into the regular script it's suppose to run without it returning true? I can't use return false;, all that does is prevent the rest of my script from running and also prevents my form from submitting. In effect, I'll just be hitting the submit button for fun.

Link to comment
Share on other sites

The IF ... ELSE IF statements are not in any kind of loop they are in a function and runs one time when a user presses the submit button. I want my nested IF statement to return back to the script and complete it, but it's not doing that. This script is validating a sign up page that has drop-down boxes, input boxes, and a check box. The problem script is right in the middle of my whole validation script, when it returns true, it skips the rest of the validation and submits the form, but I don't want that.

<!--function validate_email(field,alerttxt){  with (field)  {    apos=value.indexOf("@")    dotpos=value.lastIndexOf(".")    if (apos<1||dotpos-apos<2) {      alert(alerttxt);      return false;    }    else {      return true;    }  }}function validate_email_char(field,alerttxt){  with (field)  {    illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;          if (field.value.match(illegalChars)) {      alert(alerttxt);      return false;    }    else {      return true;    }  }}function chkNumeric(value) {  if (!value.toString().match(/^\d*\d*$/)) return false;  return true;}function validate_form(thisform){  with (thisform)  {    var getid = document.getElementById("sign_up");    var num = document.getElementById("sign_up").hid_val_num.value;    if (getid.plan.selectedIndex == 0) {      alert("Please select your hosting plan.");      plan.focus();      return false;    }    else if ((getid.plan.selectedIndex == 2) && (getid.domain.value == "")) {      alert("Please enter in a domain name.");      document.getElementById("domain").style.display = "block";      domain.focus();      return false;    }    else if ((getid.plan.selectedIndex == 3) && (getid.domain.value == "")) {      alert("Please enter in a domain name.");      document.getElementById("domain").style.display = "block";      domain.focus();      return false;    }    else if ((getid.plan.selectedIndex == 2) && (getid.ext.selectedIndex == 0)) {      alert("Please select a domain extension.");      ext.focus();      return false;    }    else if ((getid.plan.selectedIndex == 3) && (getid.ext.selectedIndex == 0)) {      alert("Please select a domain extension.");      ext.focus();      return false;    }    else if (getid.ctype.selectedIndex == 0) {      alert("Please select a card type.");      ctype.focus();      return false;    }    else if (getid.cnumber.value == "") {      alert("Please enter in the card number.");      cnumber.focus();      return false;    }    else if (chkNumeric(getid.cnumber.value) == false) {      alert("Only numbers are allowed for the card number. No spaces, dashes or periods.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 1) && (getid.cnumber.value.length != 16)) {      alert("Number length for the Discover card does not match 16 digits.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 2) && (getid.cnumber.value.length != 15)) {      alert("Number length for the American Express card does not match 15 digits.");      cnumber.focus();      return false;    }    else if ((getid.ctype.selectedIndex == 3) && (getid.cnumber.value.length != 16)) {      alert("Number length for the Master Card does not match 16 digits.");      cnumber.focus();      return false;    }    else if (getid.ctype.selectedIndex == 4) {      if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) {        alert("Number lengh for the Visa card does not match 13 or 16 digts.");        cnumber.focus();        return false;      }    }    else if (getid.cmonth.selectedIndex == 0) {      alert("Please select the expiration month.");      cmonth.focus();      return false;    }    else if (getid.cyear.selectedIndex == 0) {      alert("Please select the expiration year.");      cyear.focus();      return false;    }    else if (getid.c3num.value == "") {      alert("Please enter in the 3-digit security code number. It is usually located on the back of your card.");      c3num.focus();      return false;    }    else if (getid.c3num.value.length != 3) {      alert("Security code number is not equal to 3 digits.");      c3num.focus();      return false;    }    else if (chkNumeric(getid.c3num.value) == false) {      alert("Security code can only have digits.");      c3num.focus();      return false;    }    else if (getid.username.value == "") {      alert("Please enter in a username.");      username.focus();      return false;    }    else if (getid.fname.value == "") {      alert("Please enter in your first name.");      fname.focus();      return false;    }    else if (getid.lname.value == "") {      alert("Please enter in your last name.");      lname.focus();      return false;    }    else if (getid.email.value == "") {      alert("Please enter in your e-mail address.");      email.focus();      return false;    }    else if (validate_email(email,"Not a valid e-mail address.") == false) {      email.focus();      email.select();      return false;    }    else if (validate_email_char(email,"E-mail contains illegal characters. It can't contain any of the following:\n\n \\ \/ [ ] ( ) < > , ; : \"  ") == false) {      email.focus();      return false;    }    else if (getid.country.selectedIndex == 0) {      alert("Please select your country.");      country.focus();      return false;    }    else if (getid.val_num.value == "") {      alert("Please enter in the validation number: \"" +num+ "\"");      val_num.focus();      return false;    }    else if (getid.val_num.value != getid.hid_val_num.value) {      alert("Numbers do not match. Please enter the correct validation number.");      val_num.focus();      return false;    }    else if (getid.terms.checked == false) {      alert("Please check the Terms box if you agree with the Terms.");      terms.focus();      return false;    }  }}function displayDiv(){  document.getElementById("domain").style.display = "block";}function hideDiv(){  document.getElementById("domain").style.display = "none";}// -->
Link to comment
Share on other sites

I don't see any statements to return true from that function, you should have one at the end of it though so that it returns true if it hasn't already returned false. But I'm not sure what the problem is, if you don't want the function to return true at a certain point then don't put a return statement there.

Link to comment
Share on other sites

lol, I know, the script is automatically returning true. When the script reaches this point, it runs the nested IF statement and jumps out and returns true even though I don't set it to return true any where. When I get back home, I will provide you an example page where you'll know exactly what I'm talking about.

    else if (getid.ctype.selectedIndex == 4) {      if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) {        alert("Number lengh for the Visa card does not match 13 or 16 digts.");        cnumber.focus();        return false;      }      [color="green"]// some how returns true here[/color]    }

I don't know why it's doing this, but if I put another script inside the ELSE IF, my script runs just fine. Look at the code below, this works.

    else if (getid.ctype.selectedIndex == 4) [color="#FF0000"]{[/color]      if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) [color="black"]{[/color]        alert("Number lengh for the Visa card does not match 13 or 16 digts.");        cnumber.focus();        return false;      [color="black"]}[/color]      else if (getid.cmonth.selectedIndex == 0) [color="black"]{[/color]        alert("Please select the expiration month.");        cmonth.focus();        return false;      [color="black"]}[/color]    [color="#FF0000"]}[/color]
Link to comment
Share on other sites

Oh, I realize what you're talking about. It's not returning true, it doesn't return anything though. The reason is because of all the else statements. The execution only follows one path in the if statement, it's not going to go back and start at the top. When you have stuff like this:

	else if ((getid.ctype.selectedIndex == 3) && (getid.cnumber.value.length != 16)) {	  alert("Number length for the Master Card does not match 16 digits.");	  cnumber.focus();	  return false;	}	else if (getid.ctype.selectedIndex == 4) {	  if ((getid.cnumber.value.length != 13) && (getid.cnumber.value.length != 16)) {		alert("Number lengh for the Visa card does not match 13 or 16 digts.");		cnumber.focus();		return false;	  }	}	...

If the getid.ctype.selectedIndex == 4 condition is true, then execution will drop into that block and evaluate the next if statement (the nested one). Regardless of whether or not that statement evaluates to true or false, the execution will stop. It won't go back to the parent if, and then execute the next else. Do you understand what I'm saying? If getid.ctype.selectedIndex == 4 is true then execution stops, regardless of whether or not the nested if evaluates to true or false.To fix this, you could just put the 2 nested conditions into the parent statement, and remove the nested statement altogether, but a better way would be to just remove all the else statements. This:

if (getid.plan.selectedIndex == 0) {	  alert("Please select your hosting plan.");	  plan.focus();	  return false;	}	else if ((getid.plan.selectedIndex == 2) && (getid.domain.value == "")) {	  alert("Please enter in a domain name.");	  document.getElementById("domain").style.display = "block";	  domain.focus();	  return false;	}	else if ((getid.plan.selectedIndex == 3) && (getid.domain.value == "")) {	  alert("Please enter in a domain name.");	  document.getElementById("domain").style.display = "block";	  domain.focus();	  return false;	}	else if ((getid.plan.selectedIndex == 2) && (getid.ext.selectedIndex == 0)) {	  alert("Please select a domain extension.");	  ext.focus();	  return false;	}

And this:

if (getid.plan.selectedIndex == 0) {	  alert("Please select your hosting plan.");	  plan.focus();	  return false;	}	if ((getid.plan.selectedIndex == 2) && (getid.domain.value == "")) {	  alert("Please enter in a domain name.");	  document.getElementById("domain").style.display = "block";	  domain.focus();	  return false;	}	if ((getid.plan.selectedIndex == 3) && (getid.domain.value == "")) {	  alert("Please enter in a domain name.");	  document.getElementById("domain").style.display = "block";	  domain.focus();	  return false;	}	if ((getid.plan.selectedIndex == 2) && (getid.ext.selectedIndex == 0)) {	  alert("Please select a domain extension.");	  ext.focus();	  return false;	}

Are going to both do the same thing. The other if statements aren't going to get executed if an error gets found because the return statement will stop the function at that point.

Link to comment
Share on other sites

I understand. What I did was just move ELSE IF bracket all the way down. But now some of my other card types fall through,but it's something that I can fix without help ... hopefully. Would it be the same if I just have my nested IF statement in function that JavaScript can call in the parent ELSE IF? Would that work also?

Link to comment
Share on other sites

You could, but it sounds like you're complicating things. With an if/else structure the browser will only execute one block of code and do one or more tests, usually it's more useful to be able to execute multiple blocks of code depending on the test results, so that's why I think it's better to get rid of the else statements altogether and just use a series of if statements. It's not going to take any longer to execute and it gives you a lot more flexibility.

Link to comment
Share on other sites

Thanks a lot man, you've been a great help. I will take into account everything you've suggested. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...