IsaacWeathers
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.P airMe.valu e=="Yes")
{
if(document.Registration.P air_FirstN ame.value= ="")
{
alert("Please enter the First Name of your partner.");
return false;
}
}
}
</script>
Thanks!
Here's my first attempt at doing it, not sure where my problem is.
<script language="javascript">
function validateForm()
{
if(document.Registration.P
{
if(document.Registration.P
{
alert("Please enter the First Name of your partner.");
return false;
}
}
}
</script>
Thanks!
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.valu e=="Yes")
{
if(Registation.Pair_FirstN ame.value= ="")
{
Registation.Pair_FirstName .focus();
alert("Please enter the First Name of your partner.");
return false;
}
}
return true;
}
//-->
</script>
<form action="Conf_Order_Confirm ation.asp" method="POST" name="Registration" id="Registration" onSubmit="return validateForm(Registration) ;">
BTW... PairMe is a radio button, not sure if that makes any difference.
I tried that code and it didn't work...
<script language="javascript" type="text/javascript">
<!--
function validateForm(Registration)
{
if(Registation.PairMe.valu
{
if(Registation.Pair_FirstN
{
Registation.Pair_FirstName
alert("Please enter the First Name of your partner.");
return false;
}
}
return true;
}
//-->
</script>
<form action="Conf_Order_Confirm
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.foc us();
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>
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.
{
objForm.Pair_FirstName.foc
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>
ASKER
Oh okay, what if I have two radio buttons (i.e. Yes and No)? Should it be
if(objForm.PairMe[0].check ed)?
if(objForm.PairMe[0].check
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].checke d)
{
var rad_val = objForm.radioBtn[i].value;
}
}
or if(!objForm.PairMe.checked
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].checke
{
var rad_val = objForm.radioBtn[i].value;
}
}
ASKER
I just tried...
<script language="javascript" type="text/javascript">
<!--
function validateForm(Registration)
{
if(Registation.PairMe[0].c hecked)
{
if(Registation.Pair_FirstN ame.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.
<script language="javascript" type="text/javascript">
<!--
function validateForm(Registration)
{
if(Registation.PairMe[0].c
{
if(Registation.Pair_FirstN
{
Registation.Pair_FirstName
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)">
<form method=post onsubmit="return anotherfunction() && validateForm(this)">
ASKER
works perfectly! thank you very much for all of your help sajuks!
ASKER
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].check ed)
{
if(objForm.Pair_FirstName. value=="")
{
objForm.Pair_FirstName.foc us();
alert("Please enter the First Name of your partner.");
return false;
}
if(objForm.Pair_FirstName. value=="")
{
objForm.Pair_LastName.focu s();
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?
<script language="javascript" type="text/javascript">
<!--
function validateForm(objForm)
{
if(objForm.PairMe[0].check
{
if(objForm.Pair_FirstName.
{
objForm.Pair_FirstName.foc
alert("Please enter the First Name of your partner.");
return false;
}
if(objForm.Pair_FirstName.
{
objForm.Pair_LastName.focu
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.v alue=="")
the second time
if(objForm.Pair_FirstName.
should be
if(objForm.ur otherfield name.value=="")
so from ur code thats
if(objForm.Pair_LastName.v
ASKER
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].chec ked)
{
if(objForm.Pair_FirstName. value=="")
{
alert("Please enter your Partner's First Name.");
return false;
}
if(objForm.Pair_LastName.v alue=="")
{
alert("Please enter your Partner's Last Name.");
return false;
}
}
return true;
}
</script>
<script language="javascript">
function validateForm(objForm)
{
if(!objForm.PairMe[1].chec
{
if(objForm.Pair_FirstName.
{
alert("Please enter your Partner's First Name.");
return false;
}
if(objForm.Pair_LastName.v
{
alert("Please enter your Partner's Last Name.");
return false;
}
}
return true;
}
</script>
ASKER
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
https://www.experts-exchange.com/questions/21743616/Javascript-Validation-Loop.html
ASKER
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].chec ked)
{
if(objForm.Pair_FirstName. value=="")
{
alert("Please enter your Partner's First Name.");
return false;
}
if(objForm.Pair_LastName.v alue=="")
{
alert("Please enter your Partner's Last Name.");
return false;
}
}
return true;
}
</script>
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].chec
{
if(objForm.Pair_FirstName.
{
alert("Please enter your Partner's First Name.");
return false;
}
if(objForm.Pair_LastName.v
{
alert("Please enter your Partner's Last Name.");
return false;
}
}
return true;
}
</script>
try the below
<script language="javascript" type="text/javascript">
<!--
function validateForm(objForm)
{
if(objForm.PairMe.value=="
{
if(objForm.Pair_FirstName.
{
objForm.Pair_FirstName.foc
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>