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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested IF statements 3

Status
Not open for further replies.

timb94

Programmer
Joined
Apr 20, 2004
Messages
58
Location
US
Is it possible to nest IF statements in Javascript?

I'm talking about the following:
IF a=b
Do Something
IF b=c
Do Something
Else
do something.

I have the following script:
if(validateName(document.HMSTAPP.lastname.value)==false)
{
document.HMSTAPP.lastname.style.backgroundColor = 'yellow';
if(blnErrorFound == false)
{
alert("Enter a Last Name.");
document.HMSTAPP.lastname.focus();
blnErrorFound = true;
}
else
{
document.HMSTAPP.lastname.style.backgroundColor = 'white';
}
}

The process should be if the first IF statement is false then change the textbox to yellow and check the second IF statement. If the first IF statement is true then bypass changing the box to yellow and the inner IF and set the textbox to white.

I also tried ELSEIF {document....} and that didn't work. I'm assuming the ELSE statement is being associated with the inner IF.

Is there something I'm missing in the inner IF (eg. end if).

Thanks for any ideas or suggestions on how this may be accomplished?
 
Yes they can work this way, you just have a closing tag in the wrong place.
Code:
if(validateName(document.HMSTAPP.lastname.value)==false)
{
  document.HMSTAPP.lastname.style.backgroundColor = 'yellow';
  if(blnErrorFound == false)
  {
    alert("Enter a Last Name.");
    document.HMSTAPP.lastname.focus();
    blnErrorFound = true;
  }
[COLOR=green]}[/color]
else
{
  document.HMSTAPP.lastname.style.backgroundColor = 'white';
}

Stamp out, eliminate and abolish redundancy!
 
your syntax looks correct. i suggest throwing in an alert for debugging to see what the value of blnErrorFound is before the inner if.

you haven't shown what is initializing blnErrorFound - i suspect the error could be here.

also, when dealing with boolean functions and vars, you can use shorter syntax as below:
Code:
if([highlight][!]![/!][/highlight]validateName(document.HMSTAPP.lastname.value)) {
	document.HMSTAPP.lastname.style.backgroundColor = 'yellow';
	
	[highlight][!]alert("blnErrorFound: " + blnErrorFound);[/!][/highlight]
	if([highlight][!]![/!][/highlight]blnErrorFound) {
		alert("Enter a Last Name.");
		document.HMSTAPP.lastname.focus();
		blnErrorFound = true;
	}
	else {
		document.HMSTAPP.lastname.style.backgroundColor = 'white';
	}
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
theniteowl is correct - that would make a difference too!

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Thanks for your quick replys.

After coding in COBOL, dBase, VB.Net and Access for many years, I've just started working in ASP and Javascript and trying to learn the syntaxs of both.

To theniteowl: I figured it was a simple syntax problem but couldn't come up with any samples here or google.

To jemminger: Thanks for the tip using the !. I was not aware of it.

Stars to both of you.

Thanks again.
 
Actually your syntax was correct but it led to a logic problem.
As it was, both color changes were within the same block so if the first if statement did not evaluate true no color change would take place.
I realized after I posted that if your intent was to have both color changes inside that block then my response was wrong but from what I can see of your code that did not seem to be the case.

Glad it's working for you.

Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top