473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Nesting ifs?

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
5 1556
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hertz | last post by:
I have hundreds of samples of XML documents on my harddisc. But all of them lack the one or another feature of XML. Some XML documents have no attributes some others are rather flat (nesting...
0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
5
by: jack | last post by:
Hi I wanna know if nesting of tags inside similar tags is possible in XML. To be more clear, is <person> <son> <person>
4
by: kl.vanw | last post by:
I would like to count the nesting level in template classes. How can I make the following work? #include <assert.h> template <class T> class A { public: A() { // what goes here?
12
by: TristaSD | last post by:
Hi, Here's a nice footer I get inside every php page I write in wwwroot on my server. The code gets parsed just fine. I installed php5.2-win32 under W2K Server, IIS 5.0. I've installed php on...
6
by: stephen.cunliffe | last post by:
Hi, I'm looking for opinion/facts/arguments on the correct nesting of UL, OL, & LI elements. For example, this is what I want (unordered list): * Item 1 * Item 2 * Item 3
17
by: henry | last post by:
Folks Here's a skeleton, generic HTML page, call it "index.php". You'll see a bit of php code in the middle: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.