Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statment within an if statment...nesting if statements 1

Status
Not open for further replies.

meriwether

Programmer
Mar 7, 2003
23
US
is is possible to nest if statments? it seems somewhat logical, but i cannot get this to work properly.

here's a snippit of the code:

// Phone Number Validation

if(phone != "Phone Number") { // phone # is optional
if(!validatePhone(phone)) {
window.document.formname.phone.focus();
alert('Please enter a valid Phone Number');
return false;
}
} // phone # is optional

else {

// Email Address Validation

if(emailaddress != "Email Address") { // email address is optional
if(!validateEmail(emailaddress)) {
window.document.formname.emailaddress.focus();
alert('Please enter a valid Email Address');
return false;
}
} // email address is optional

else {

// Passed Validation

return true; // form validation passed

}
}

any pointers on nesting if statements would be greatly appreciated. need more info, i'll post all the code i suppose. thx.
 
your syntax is legal, but here's your current logic:

if(phone != "Phone Number") {
// validate the phone number
}
else {
if(emailaddress != "Email Address") {
// if no phone number but yes email, validate the email address
}
else {
// if no email or phone, return true
}
}


what you're looking for is probably something like so, and does not require nesting:

if(phone != "Phone Number") {
// validate the phone number
if (invalid phone) return false;
}

if(emailaddress != "Email Address") {
// validate the email address
if (invalid email) return false;
}
// did not fail above, so data ok
return true;


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
thank you jeff.
that totally solved my problem.
much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top