Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Nesting ifs?

Reply
Thread Tools

Nesting ifs?

 
 
Garry Jones
Guest
Posts: n/a
 
      10-06-2006
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



 
Reply With Quote
 
 
 
 
Touffy
Guest
Posts: n/a
 
      10-06-2006
On 2006-10-06 16:22:35 +0200, "Garry Jones" <(E-Mail Removed)> said:

> 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

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      10-06-2006
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[]']
 
Reply With Quote
 
Garry Jones
Guest
Posts: n/a
 
      10-06-2006
"Touffy" <(E-Mail Removed)> skrev i meddelandet
news:452668b2$0$1504$(E-Mail Removed)...
> On 2006-10-06 16:22:35 +0200, "Garry Jones" <(E-Mail Removed)> said:
>
>> 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


 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-07-2006
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

 
Reply With Quote
 
Garry Jones
Guest
Posts: n/a
 
      10-07-2006
Touffy" <(E-Mail Removed)> skrev i meddelandet
news:452790e6$0$24061$(E-Mail Removed)...

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Module.nesting -> Kernel#nesting Trans Ruby 10 09-16-2005 12:21 AM
group nesting David K MCSE 6 12-12-2003 04:54 PM
Nesting Datagrids Edward ASP .Net 3 11-11-2003 09:22 PM
Nesting Datagrids John ASP .Net 0 11-11-2003 04:49 PM
Nesting object tags in ASP 3.0, ASP.NET & Internet Explorer DeadlyTedly ASP .Net 2 10-16-2003 08:32 AM



Advertisments