By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,568 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,568 IT Pros & Developers. It's quick & easy.

Nesting ifs?

P: n/a
I want to trigger an alert if three conditons are met.

How do I nest if statements? I tried this, but it triggered the alert if
just one of the three conditions was met.

function checkform ( form )
{
if (form.blue.value == "yes") {
if (form.green.value == "yes") {
if (form.yellow.value == "yes") {
alert( "You can only choose two items for this option");
form.dtet1.focus();
return false ;
}
}
}
return true ;
}

Any help greatly appreciated

Garry Jones
Sweden

Oct 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2006-10-06 16:22:35 +0200, "Garry Jones" <ga*********@morack.sesaid:
I want to trigger an alert if three conditons are met.
you could just combine them in a single if, which would be more
efficient, and would save a lot of {}'s :

if(form.blue.value == "yes" && form.green.value == "yes"
&& form.yellow.value == "yes")
function checkform ( form )
{
if (form.blue.value == "yes") {
if (form.green.value == "yes") {
if (form.yellow.value == "yes") {
alert( "You can only choose two items for this option");
form.dtet1.focus();
return false ;
}
}
}
return true ;
}
your code seems correct though. Can't see why it fails. Maybe the
values of your form inputs are initially set to "yes" ?
--
David Junger

Oct 6 '06 #2

P: n/a
Garry Jones wrote:

[snip]
How do I nest if statements?
Precisely the way you posted, though in the posted case, you can just
use a single if statement and combine the conditions with the logical
AND operator (&&).
I tried this, but it triggered the alert if just one of the three
conditions was met.
[snip]
if (form.blue.value == "yes") {
Is the control a checkbox? If so, it's value will always be the same
regardless of whether the control is checked. You should check the
checked property.
if (form.green.value == "yes") {
if (form.yellow.value == "yes") {
Another consideration is that if all of these represent values within a
common category (colours, here), give the checkboxes the same name[1]
and use their values to indicate the option chosen:

<input name="colour" type="checkbox" value="blue">
<input name="colour" type="checkbox" value="green">
<input name="colour" type="checkbox" value="yellow">

To check the number of checked controls within that group, loop through
the resulting collection:

function countCheckedControls(controlGroup) {
var count = 0;

for (var i = 0, n = controlGroup.length; i < n; ++i)
if (controlGroup[i].checked) ++count;
return count;
}

var checkedColours = countCheckedControls(form.elements.colour);

[snip]

Mike
[1] For PHP, the name would be "colour[]". This will create an
array within the $_GET superglobal array with the name
"colour". Each element of this will contain the values of the
checked checkboxes. To use that name in a script, use bracket
notation:

form.elements['colour[]']
Oct 6 '06 #3

P: n/a
"Touffy" <tf**@free.frskrev i meddelandet
news:45**********************@news.free.fr...
On 2006-10-06 16:22:35 +0200, "Garry Jones" <ga*********@morack.sesaid:
>I want to trigger an alert if three conditons are met.

you could just combine them in a single if, which would be more efficient,
and would save a lot of {}'s :
Thanks for your answers, problem persisting.

I simplified my code to give you guys a simple example. I put the && in my
code but it is still misfiring. This time I post the actuall code.

The values come from several checkboxes which come from different groups of
checkboxes. I need to check for a certain conditon where five checkboxes
have been clicked. Checkboxes that have been clicked pass on the value of
"ja" . This works on the subsequent processing form and I can sucessfully
test for all "ja" values there and unchecked boxes are not returning "ja". I
can back the user from the processing form but would prefer to catch this
condition with java script.

They do not form part of an array because they come from different
selections of buttons in other catergoized groups. So why is this not
working? It never triggers the alert.

if(form.dtet1.value == "ja" && form.bil1.value == "ja" && form.mmm33.value
== "ja" && form.kitt2.value == "ja" && form.dtet2.value == "ja")
alert( "warning text" );
form.dtet1.focus();
return false ;

This is an example of one of the checkboxes.
<input name="dtet1" type="checkbox" class="dt_inp" id="dtet1" title="dtet1"
value="ja">

This returns "ja" when clicked. But how do I trap those 5 values with java?

Any help very greatfully appreciated.

Garry Jones
Sweden
Oct 7 '06 #4

P: n/a
Garry Jones said:
The values come from several checkboxes which come from different
groups of checkboxes. I need to check for a certain conditon where five
checkboxes have been clicked. Checkboxes that have been clicked pass on
the value of "ja" . This works on the subsequent processing form and I
can sucessfully test for all "ja" values there and unchecked boxes are
not returning "ja". I can back the user from the processing form but
would prefer to catch this condition with java script.
How do you determine that they do not return "ja" when not checked ?
Do you conclude that just because the server receives a form result
without the values of unchecked boxes ?

Under certain conditions, a form field will not be sent to the form's
action. That happens when the field is disabled, unchecked (for
checkboxes), or not selected (radios and select options).
Not being sent doesn't prevent it from being an HTML element with
attributes that can be accessed by client-side scripts.
if(form.dtet1.value == "ja" && form.bil1.value == "ja" &&
form.mmm33.value == "ja" && form.kitt2.value == "ja" &&
form.dtet2.value == "ja")
alert( "warning text" );
form.dtet1.focus();
return false ;

This is an example of one of the checkboxes.
<input name="dtet1" type="checkbox" class="dt_inp" id="dtet1"
title="dtet1" value="ja">
This code clearly sets the value of the checkbox to "ja". Look no further.
What you have to test is whether the checkbox's checked attribute is
true, not whether its value is "ja".

Replace every '.value == "ja"' in your test by '.checked == true' (or
just '.checked') and it should work.
This returns "ja" when clicked. But how do I trap those 5 values with java?
with Java, I have no idea. We're talking JavaScript, right ?
--
David Junger

Oct 7 '06 #5

P: n/a
Touffy" <tf**@free.frskrev i meddelandet
news:45***********************@news.free.fr...
How do you determine that they do not return "ja" when not checked ?
Because on the processing form I use this code on those values.

if ($var == "ja"){
$dtmoney = $dtmoney + 350;
}

Where $var is each and everyone of those values.
Do you conclude that just because the server receives a form result
without the values of unchecked boxes ?
Well, yes the variable $dtmoney fires correctly and only adds 350 with the
checkboxes that have been checked. ie it is those and only those boxes that
pass the value of ja. The other checkboxes return null.
This code clearly sets the value of the checkbox to "ja". Look no further.
What you have to test is whether the checkbox's checked attribute is true,
not whether its value is "ja".
Thats new to me. As far as php is concerned the value is only "ja" if the
checkbox is checked. I thought javascript would treat this in the same way
and I can not understand how Javascript sees a "ja" when php sees a "null".
Obviously it does so I am pleased that you have taking the time to explain
this to me.
Replace every '.value == "ja"' in your test by '.checked == true' (or just
'.checked') and it should work.
Ok, great, thanks for your help. I will do that now.

Garry Jones
Sweden
Oct 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.