Link to home
Start Free TrialLog in
Avatar of IsaacWeathers
IsaacWeathersFlag for United States of America

asked on

JavaScript nested IF statement?

I am new to javascript, but I would assume that there is a way to nest IF statements, correct? I am trying to setup some form validation based upon whether a previous form element was set to Yes. So, in the example below, if Form("PairMe") = "Yes" then I want to require that they fill in Form("Pair_FirstName"). Is that possible with javascript?

Here's my first attempt at doing it, not sure where my problem is.

<script language="javascript">
function validateForm()
{
      if(document.Registration.PairMe.value=="Yes")
      {
            if(document.Registration.Pair_FirstName.value=="")
            {
            alert("Please enter the First Name of your partner.");
            return false;
            }
      }
}
</script>

Thanks!
Avatar of sajuks
sajuks

what error r u getting ?
try the below
<script language="javascript" type="text/javascript">
<!--
function validateForm(objForm)
{
     if(objForm.PairMe.value=="Yes")
     {
          if(objForm.Pair_FirstName.value=="")
          {
          objForm.Pair_FirstName.focus();
          alert("Please enter the First Name of your partner.");
          return false;
          }
     }
     return true;
}

//-->
</script>
</head>

<body>
<form method=post onsubmit="return validateForm(this)">
<input type="text" name="PairMe" id="PairMe">
<input type="text" name="Pair_FirstName" id="Pair_FirstName">
<input type="submit" name="SubmitMe" id="SubmitMe" value ="Save">
</form>
Avatar of IsaacWeathers

ASKER

I'm not getting an error, just no Alert requesting that I validate the form and it's also submiting the form instead of failing and return false back to the page.

I tried that code and it didn't work...

<script language="javascript" type="text/javascript">
<!--
function validateForm(Registration)
{
     if(Registation.PairMe.value=="Yes")
     {
          if(Registation.Pair_FirstName.value=="")
          {
          Registation.Pair_FirstName.focus();
          alert("Please enter the First Name of your partner.");
          return false;
          }
     }
     return true;
}

//-->
</script>

<form action="Conf_Order_Confirmation.asp" method="POST" name="Registration" id="Registration" onSubmit="return validateForm(Registration);">

BTW... PairMe is a radio button, not sure if that makes any difference.
"BTW... PairMe is a radio button, not sure if that makes any difference."
it does....
how many radio buttond do u've in ur code ?when value ="Yes" means when it is checked ??
assuming u r checking for only one rb and u need the value to be validated when its checked....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>  RB</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
<!--
function validateForm(objForm)
{

     if(objForm.PairMe.checked)
     {
          if(objForm.Pair_FirstName.value=="")
          {
              objForm.Pair_FirstName.focus();
          alert("Please enter the First Name of your partner.");
          return false;
          }
     }
       return true;
}

//-->
</script>
</head>

<body>
<form method=post onsubmit="return validateForm(this)">
<input type="radio" name="PairMe" id="PairMe">
<input type="text" name="Pair_FirstName" id="Pair_FirstName">
<input type="submit" name="SubmitMe" id="SubmitMe" value ="Save">
</form>
</body>
</html>
Oh okay, what if I have two radio buttons (i.e. Yes and No)? Should it be

if(objForm.PairMe[0].checked)?
if its only two u use the else part...
or if(!objForm.PairMe.checked) //<<notice the exclamation mark at the start..thats signifies NOT

if u've multiple radiobuttons with the same name and u need to find which value was selected..u need to have a loop for it..


for (var i=0; i < objForm.radioBtn.length; i++)
   {
   if (objForm.radioBtni].checked)
      {
      var rad_val = objForm.radioBtn[i].value;
      }
   }
I just tried...

<script language="javascript" type="text/javascript">
<!--
function validateForm(Registration)
{
     if(Registation.PairMe[0].checked)
     {
          if(Registation.Pair_FirstName.value=="")
          {
          Registation.Pair_FirstName.focus();
          alert("Please enter the First Name of your partner.");
          return false;
          }
     }
     return true;
}

//-->
</script>

<form method=post onsubmit="return validateForm(Registration)">
<input type="radio" name="PairMe" id="PairMe" value="Yes">
<input type="radio" name="PairMe" id="PairMe" value="No">
<input type="text" name="Pair_FirstName" id="Pair_FirstName">
<input type="submit" name="SubmitMe" id="SubmitMe" value ="Save">
</form>


and that doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok - that fixed it. Thanks! Also, I have another pre-packaged validation onSubmit that is occurring, how do I get it to return each individually? right now, according to where i place the "return validateForm(this)" (either before or after the pre-packaged validation) it either will or will not return the correct variable. example: validateForm(this) found no errors, however the pre-packaged validation found errors. it will pop up the alerts for the pre-packaged validation, however it will then Submit the form because the return = true for the validateForm(this) validation. Make sense? If i need to post this as a second question just let me know. Thanks for all of your help!
this would work
<form method=post onsubmit="return anotherfunction() && validateForm(this)">
works perfectly! thank you very much for all of your help sajuks!
I am very sorry, I assumed that once I got the initial validation down then i could just recreate that for the rest of the Form objects. i.e. I have more than the Pair_FirstName that I need to validate. I tried the following...

<script language="javascript" type="text/javascript">
<!--
function validateForm(objForm)
{
     if(objForm.PairMe[0].checked)
     {
          if(objForm.Pair_FirstName.value=="")
          {
          objForm.Pair_FirstName.focus();
          alert("Please enter the First Name of your partner.");
          return false;
          }
         
          if(objForm.Pair_FirstName.value=="")
          {
          objForm.Pair_LastName.focus();
          alert("Please enter the Last Name of your partner.");
          return false;
          }
     }
     return true;
}

//-->
</script>

But that didn't work, any thoughts? or do I need to post this as another question?
maybe a cut and paste error ;-)
the second time
if(objForm.Pair_FirstName.value=="")
should be
if(objForm.ur otherfield name.value=="")
so from ur code thats
if(objForm.Pair_LastName.value=="")

Sorry, yes i copied before i had made the change. Even after the change it's still only poping the alert for the FirstName.

<script language="javascript">
function validateForm(objForm)
{
      if(!objForm.PairMe[1].checked)
      {
            if(objForm.Pair_FirstName.value=="")
            {
            alert("Please enter your Partner's First Name.");
            return false;
            }      
            if(objForm.Pair_LastName.value=="")
            {
            alert("Please enter your Partner's Last Name.");
            return false;
            }                  
      }
      return true;
}
</script>
sajuks, i know the last question was not a part of my original question, so i have posted it again as a new question.  thanks for all of your help!

https://www.experts-exchange.com/questions/21743616/Javascript-Validation-Loop.html 
Evidentialy no one wants to respond to my other question. sajuks, do you have any ideas on what i am doing wrong?

https://www.experts-exchange.com/questions/21743616/Javascript-Validation-Loop.html 

I am trying to validate several form objects based upon the selection of a radio button. I have the validation down, now I just need to "loop" through all of the fields that I want to require (i.e. FirstName, LastName, Company, etc...). I'm not sure exactly how this should be done.

Here's how I have tried to accomplish this...

<script language="javascript">
function validateForm(objForm)
{
     if(!objForm.PairMe[1].checked)
     {
          if(objForm.Pair_FirstName.value=="")
          {
          alert("Please enter your Partner's First Name.");
          return false;
          }    
          if(objForm.Pair_LastName.value=="")
          {
          alert("Please enter your Partner's Last Name.");
          return false;
          }              
     }
     return true;
}
</script>