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

If statement not completing...

Status
Not open for further replies.

lidds

Programmer
Jun 9, 2005
72
GB
Hi I am very new to javascript but can't seem to find the anwser on the net, so thought I would post here.

I have an if statement that for some reason runs the first and second if statement but does not process the third if statement or the return statement, can someone point out what I am doing wrong..

Code:
function registerValidate() {

// does this if statement
 if (document.frmRegister.txtFirstname.value == '' || document.frmRegister.txtLastname.value == '')
 {
        // does this if statement
	if (document.frmRegister.txtFirstname.value == '')
	{
		document.frmRegister.txtErrFirstName.value = 'Required'		
	}
	
        // does not do this if statement
	if (document.frmRegister.txtLastname.value == '')
	{
		document.frmRegister.txtErrLastName.value = 'Required'
	}	
	
        // does not do this return statement
	return false;
 }
 else
 {
  return true;
 }

}

thanks

Simon
 
Let's break it down a little bit and simplify the code:
Code:
function registerValidate()
{
var el = document.forms['frmRegister'].elements;

if (el['txtFirstname'].value == '')
  {
  el['txtErrFirstName'].value = 'Required';
  return false;        
  }
    
if (el['txtLastname'].value == '')
  {
  el['txtErrLastName'].value = 'Required'
  return false;
  }    

return true;
}

If that doesn't work, make sure all the form element names are spelled EXACTLY like what you have in the function.

You don't need to check twice if the last name and first name are filled in. I'd suspect that your last name element, or the last name error element, aren't named what you have in the Javascript code. You have Name with an upper case N sometimes, and sometimes with a lower case n.

Lee
 
If you take out:

Code:
        // does this if statement
    if (document.frmRegister.txtFirstname.value == '')
    {
        document.frmRegister.txtErrFirstName.value = 'Required'        
    }

does the txt.Lastname.value statement work? If so, then its just a syntax problem. Shouldn't it be...

Code:
if statement {
  if statement {
  }
  else if statement{
  }
  return false;
}
else {
  return true;
}
 
Lee,

You where right typo error had txtErrLastName rather than txtErrLastname, good night sleep and fresh eyes always help I think.

Thanks for the help guys

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top