...

View Full Version : Nesting if statements



jwindon
01-08-2004, 03:59 AM
Hello. This is my very second day at programming with JavaScript. I have a fair programming background in C++ and some VB. Still an infant, yes and hope you will still help me with such a silly little problem.

I have been all over the Internet tonight and searched this forum as well without success. Basically, I just need to determine why the syntax doesn't work on this.

function Check_Data(){
if(document.REG.wc_DISTRICT.value==0)
{
alert("Please select the school district.");
document.REG.wc_DISTRICT.focus();
}
else if(document.REG.wc_FSDNAME==" "){
alert("Please provide the name of the Food Service Director for your district.");
}
}

The code above is partial. I have written the other checks that will be in this series of else if. But FOR THE LIFE OF ME! I can't get the alert to run with that else if.

Please help. I think once I get past this....I can get the others (not above) working.

Willy Duitt
01-08-2004, 04:23 AM
The non-breaking space you have between the double quotes in this statement would never allow the alert to fire if there was nothing in the input. Place your cursor in the input and press the spacebar and then submit. You should then see what I am referring to.
BTW: .value is missing also.

else if(document.REG.wc_FSDNAME.value==" "){

Either add .value and remove the space or try something like this:

var FSDNAME = document.REG.wc_FSDNAME.value;
if (!FSDNAME) {
alert("Please provide the name of the Food Service Director for your district.");
document.REG.wc_FSDNAME.focus();
return false;
}

.....Willy

jwindon
01-08-2004, 04:28 AM
Thanks Willy. I do have a default value of the actual space in the text field. I even tried changing the default value and code. No luck.

I guess my problem is that I am trying to add a series of checks within one function and can't get the syntax to work for me.

I AM able to do If...Else without problems.

What is the structure for something that runs a series of IFs inside of one function? Or should I just break down and create several separate functions; each to perform one individual task?

I'm up until I get this thing working. :confused:

jwindon
01-08-2004, 04:41 AM
This is bulky .....but will give you the idea of what I'm trying to do.

function Check_Data(){
if(document.REG.wc_DISTRICT.value==0)
{
alert("Please select the school district.");
document.REG.wc_DISTRICT.focus();
}

else if((document.REG.wc_HAVINGVENDING.value==1)&& (document.REG.wc_DAILYMILKVENDINGSALES.value==7)){
alert("You have indicated your district has milk vending machines, but have not chosen a daily sales range.");
document.REG.wc_DAILYMILKVENDINGSALES.focus();
}

else if((document.REG.wc_HAVEVENDING==1) && (document.wc_NOMILK.checked==true)){
alert("You indicated that you have milk vending machines, but have 'Not Appliable' checked in the types of milk available selections. Select the types of milk available and/or deselect the 'Not Applicable' circle.");
}

else if(document.REG.wc_OTHERMILKSERVED=="null"){
alert("You have indicated milk served as 'Other', but have not provided the specifics.");
document.REG.wc_OTHERMILKSERVED.focus();
}

else if(document.REG.wc_FSDNAME==" "){
alert("Please provide the name of the Food Service Director for your district.");
}

}

Willy Duitt
01-08-2004, 04:58 AM
Please try this:

function Check_Data() {
if(document.REG.wc_DISTRICT.value==0) {
alert("Please select the school district.");
document.REG.wc_DISTRICT.focus();
return false;
}
else {return true;}

if((document.REG.wc_HAVINGVENDING.value==1)&& (document.REG.wc_DAILYMILKVENDINGSALES.value==7)) {
alert("You have indicated your district has milk vending machines, but have not chosen a daily sales range.");
document.REG.wc_DAILYMILKVENDINGSALES.focus();
return false;
}
else {return true;}

if((document.REG.wc_HAVEVENDING==1) && (document.wc_NOMILK.checked==true)) {
alert("You indicated that you have milk vending machines, but have 'Not Appliable' checked in the types of milk available selections. Select the types of milk available and/or deselect the 'Not Applicable' circle.");
return false;
}
else {return true;}

if(document.REG.wc_OTHERMILKSERVED=="null") {
alert("You have indicated milk served as 'Other', but have not provided the specifics.");
document.REG.wc_OTHERMILKSERVED.focus();
return false;
}
else {return true;}

if(document.REG.wc_FSDNAME==" ") {
alert("Please provide the name of the Food Service Director for your district.");
return false;
}
else {return true;}
}


.....Willy

jwindon
01-08-2004, 05:03 AM
Nope

Should I send the page?

Can't get past the first edit. The District check works.

Willy Duitt
01-08-2004, 05:27 AM
You can attach it to your post.
If it is too large or has several files,
Try zipping before attaching.

.....Willy

jwindon
01-08-2004, 05:32 AM
Ok.
I tried each check alone and the only one that will fire is the first one. This is the problem I've had all night. I suspect the problem can't be found without seeing the rest of the code. I REALLY appreciate your help. I want to put this thing up tomorrow morning. I promise I will study what you come up with. Getting ready for day three of my learning with this stuff.

Willy Duitt
01-08-2004, 10:53 AM
Well one of the first problems was a typo HAVINGVENDING

Another problem was radio buttons do not have an explicit checked.value until you loop thru the radio button array to see which one was checked or you can check the state of a specific radio button by referencing its place in the array.


if((document.REG.wc_HAVEVENDING[0].checked==true)&&
(document.REG.wc_DAILYMILKVENDINGSALES[0].checked==true)) {
alert("You have indicated your district has milk vending machines,
but have not chosen a daily sales range.");
return false;
}

Below you will find an attachment including the mockup.
The validation works, But you will need to look closely at
the milk options since I added some validation to clear the
Not Applicable checkbox (changed from a radio button)
Additionally, I took the liberty to add an Auto Date script. :D

.....Willy

Edit: BTW: I also changed the input type=submit to a button and
called the form submit from within the check_Data validation because
I prefer to do it this way, however, you could fire your validation
by using onsubmit within your <form> tag.

jwindon
01-08-2004, 03:26 PM
Thanks Willy! Your work looks great to me! :o

I put your coding into my page. Thing is I have to keep all the other scripts in the page as we are required to keep the template format. The data is posted to a server versus submitted by email.

The checks you wrote work for requiring the district and for toggling what kinds of milk.

The CheckOther function that prompts for a specific is not working, nor anything past the first check again in the CheckData.

I'm supposing it is a braces problem somewhere. I'm tinkering with it, but not having too much luck. I have attached where I am at right now.

Can you see what could be goofing me up?

jwindon
01-08-2004, 03:34 PM
Ok. The CheckOther function to prompt for specifics was missing (duh!) Put that back in and it fires when it's supposed to.

But the check for =="null" (which is the result if the user hits Cancel on the prompt) does not work properly.

jwindon
01-08-2004, 04:10 PM
Got the bugs worked out in the CheckData so that they all work. The .value was overlooked in the text evaluation if statements.

Now.....to see if I can get it to work inside the rest of the code


:rolleyes:

function Check_Data() {
if(document.REG.wc_DISTRICT.value==0) {
alert("Please select the school district.");
document.REG.wc_DISTRICT.focus();
return false;
}

if((document.REG.wc_HAVEVENDING[0].checked==true) && (document.REG.wc_DAILYMILKVENDINGSALES[0].checked==true)) {
alert("You have indicated your district has milk vending machines, but have not chosen a daily sales range.");
return false;
}

if((document.REG.wc_HAVEVENDING[0].checked==true) && (document.REG.wc_NOMILK.checked==true)) {
alert("You indicated that you have milk vending machines, but have 'Not Appliable' checked in the types of milk available selections. ");
return false;
}

if(document.REG.wc_OTHERMILKSERVED.value=="j") {
alert("You have indicated milk served as 'Other', but have not provided the specifics.");
document.REG.wc_OTHERMILKSERVED.focus();
return false;
}

if(document.REG.wc_FSDNAME.value==" ") {
alert("Please provide the name of the Food Service Director for your district.");
return false;
}
else {
document.REG.submit();
return true;
}

jwindon
01-08-2004, 04:31 PM
Thanks to Willy's guiding code and some serious combing and testing, the page is working properly!!!!!!11 :D


Just a couple more enhancements.....I need help here. Having a hard time with this text field. I need to alert if the field == "some text" or IsNull? I don't know the syntax for a null value?

if(document.REG.wc_OTHERMILKSERVED.value=="null") {
alert("You have indicated milk served as 'Other', but have not provided the specifics.");
document.REG.wc_OTHERMILKSERVED.focus();
return false;
}

Willy Duitt
01-08-2004, 04:34 PM
Try replacing the statement with this:

if(document.REG.wc_HAVEMILK[6].checked==true && document.REG.wc_OTHERMILKSERVED.value.length==0) {
alert("You have indicated milk served as 'Other', but have not provided the specifics.");
document.REG.wc_OTHERMILKSERVED.focus();
return false;
}

And yes, my mockup was only specific to the necassary changes.
Considering that I was not familar with the form elements, and I
didn't want to scroll thru 35K of code, I isolated everything else
but what I needed to look at. You were supposed to work the
changes back into your document. :D

I'm glad to see you worked that out. ;)
But, I just realized, you only ready had an auto date function. :o

.....Willy

jwindon
01-08-2004, 04:37 PM
You're a real trooper Willy and I VERY! much appreciate you helping me out. I started out with tinkering with VBA for Access on a forum and before long I was teaching others too. Now I'm back to being a student and only hope that I can become as good as you (or at least semi-proficient) in this JavaScript. I'll be studying your code.

I'm putting in some other checks right now......like if they select they Do NOT having vending machines.....then they should leave the sales and milk types at Not Applicable.

:thumbsup:

jwindon
01-08-2004, 05:00 PM
Sorry....Once I learn the basics...I'm sure I won't be such a pest.

I want to have the Sales (radio group) set back to 'Not Appliable' in this code. The value of 'Not Applicable' is 7.

if((document.REG.wc_HAVEVENDING[0].checked==false) && ((document.REG.wc_NOMILK.checked==false) ||(document.REG.wc_DAILYMILKVENDINGSALES.value=1))) {
alert("You indicated that you have do NOT have milk vending machines. ");
document.REG.wc_DAILYMILKVENDINGSALES.value=7;
document.REG.wc_NOMILK.checked=true;
document.REG.wc_OTHERMILKSERVED.value="";
for (i=0; i<document.REG.wc_HAVEMILK.length; i++){
if(document.REG.wc_NOMILK.checked == true){
document.REG.wc_HAVEMILK[i].checked = false;
}
}
return false;
}

Also tried......

document.REG.wc_DAILYMILKVENDINGSALES.checked=true;

:confused:

jwindon
01-08-2004, 05:26 PM
:thumbsup:

Ok....figured out that assignment radio button thing for the sales.

Believe that is it now.....

Just one question.....
Our web collector uses the names of the input fields as the column headings in the backend data file. I noticed that you have renamed all the checkboxes in the milk type group to "wc_HAVEMILK" and with the appropriate id="...". Will the same input name cause problems for my backend? I'm not able to access it from home.

Thanks.

........input name="wc_HAVEMILK" type="checkbox" id="wc_YESSKIM2" ............

<input name="wc_HAVEMILK" type="checkbox" id="wc_YESLOWCHOC4" ............

jwindon
01-08-2004, 07:16 PM
Ok. I managed to test from home. The data goes in there....but, as I suspected...the milk types get thrown all into one field 'wc_HAVEMILK'.

like......... {1,1,1}

I could make those values unique for each milk type, but that doesn't solve my problem as I need to be able to tabulate by each type and want them to be seperate fields. Can I use some type of numbered array or something like that to name the fields and still use the for loop in the ckIt and clrIt functions?:confused:

Garadon
01-08-2004, 08:18 PM
This is only a guess not good at checkbooxes at all.

But I think the value saved is the value of the box so if you write the milks name/id as value for its representative checkbox it should save it ....I think.

jwindon
01-08-2004, 08:23 PM
almost........

I'm attempting to do this clear function the hard way...... Does it go something like this? If one checkbox is checked, then this function is called to uncheck the wc_YES...checkboxes when the first checkbox 'wc_NOMILK" is checked. Clear as mudd right?

function clrIt(){
if(document.REG.wc_NOMILK.checked == true);{
document.REG.wc_YESWHOLEUNFLAVORED2.checked = false;
document.REG.wc_YESWHOLECHOC2.checked = false;
document.REG.wc_YES2PER3.checked = false;
document.REG.wc_YES1PER3.checked = false;
document.REG.wc_YESSKIM2.checked = false;
document.REG.wc_YESLOWCHOC4.checked = false;
document.REG.wc_YESOTHERSERVED2.checked = false;
}
}

Willy Duitt
01-08-2004, 09:11 PM
Yes, I was afraid that changing the checkbox name(s) to group them together into an array as is common practice would cause you trouble. :(

But as Garadon suggested, if you change the value(s) from 1 to the name(s) you previously assigned, you can pass the name thru the value(s).

I just noticed you posted again while I have been replying.

If you proceed to name each checkbox uniquely, you will need to hard code each name rather than looping thru an array.

What is wrong with leaving the name(s) the one I had them
(which is how they should be) and pass thru your assigned values as you should?

EG: <input type=checkbox name=wc_HAVEMILK value="wc_YESLOWCHOC4=1"

.....Willy

Willy Duitt
01-08-2004, 09:15 PM
Sorry, I didn't see this:

I could make those values unique for each milk type, but that doesn't solve my problem as I need to be able to tabulate by each type and want them to be seperate fields.

Can you not split the values by type once it is in the database?

jwindon
01-08-2004, 09:19 PM
Originally posted by jwindon
Ok. I managed to test from home. The data goes in there....but, as I suspected...the milk types get thrown all into one field 'wc_HAVEMILK'.

like......... {1,1,1}

I could make those values unique for each milk type, but that doesn't solve my problem as I need to be able to tabulate by each type and want them to be seperate fields. Can I use some type of numbered array or something like that to name the fields and still use the for loop in the ckIt and clrIt functions?:confused:


I agree that using arrays and loops is certainly best practice, however, as I indicated in an earlier post (while you were having LUNCH ;) ), I need to have those values broke into separate fields. I'm a database development person by trade and this is my best practice.

Anyway, I went back and groolingly hard coded each lousy checkbox into the checkIt and clearIt functions and so forth. I'm happy to report that it seems the page is actually FINISHED!!!

Just have to check my backend files now to be sure. I'll be back shortly and let you know! You've been a tremendous help and I've learned so much in just the past 12 hours. I have another one of these to do right after this one.....so that will be my true test of what I've learned thus far.

I have attached what I think is my final version. Try it. :cool:

jwindon
01-08-2004, 09:21 PM
Ha ha Willy. You are posting while I am typing. :)

Anyway, try out that final version and let me know what you think.

We make a good team as you the teacher and me the rebellious student ey?

What a night/morning/afternoon!!!

:o

jwindon
01-08-2004, 09:35 PM
BY GEORGE!!!!!! It's done!

Checked the backend .csv file and all is structured to my liking now. New structure data file is attached.

Thanks for all the help. Now I rest. :thumbsup:

Willy Duitt
01-08-2004, 09:48 PM
What did you do?
Rescript how the backend processed the data?

Anyways, this would have worked. :D
Leaving everything the way you had it
other than adding the onclick event handlers.


var HAVEMILK = new Array(7)
HAVEMILK[0] = 'wc_YESWHOLEUNFLAVORED';
HAVEMILK[1] = 'wc_YESWHOLECHOC';
HAVEMILK[2] = 'wc_YES2PER';
HAVEMILK[3] = 'wc_YES1PER';
HAVEMILK[4] = 'wc_YESSKIM2';
HAVEMILK[5] = 'wc_YESLOWCHOC';
HAVEMILK[6] = 'wc_YESOTHERSERVED';

function chkIt(){
for (i=0; i<HAVEMILK.length; i++){
if(document.REG[HAVEMILK[i]].checked == true){
document.REG.wc_NOMILK.checked = false;
}
}
}

function clrIt(){
for (i=0; i<HAVEMILK.length; i++){
if(document.REG.wc_NOMILK.checked == true){
document.REG[HAVEMILK[i]].checked = false;
}
}
}

.....Willy

jwindon
01-08-2004, 09:53 PM
ooooh! you're killing me! lol

I'm going to study that code you just posted and try it for my next version. Gotta give up the ghost for now.

I didn't do anything to change the processing of the backend file. I loveingly put a check = true or check = false on each checkbox name in the form (after changing them to the ID value). Yeah....I know....hard way. But I was in a time crunch.

Anyway, Off here for now. Hope you're around for the next one!!! :eek:lol

;) :cool: :D :thumbsup:

Willy Duitt
01-08-2004, 09:57 PM
Things are happening to fast and between distractions here and messages being posted while I have the Post Reply open, I am missing half of what is being siad or done.

Anyway, I went back and groolingly hard coded each lousy checkbox into the checkIt and clearIt functions and so forth.

I you hard coded them all, try the last array script.
You can leave the HAVEMILK array as a global variable
and use it in both functions, or place the array in each one.

Attached is a copy.

I just sent my secretary to the post office to get
stamps I don't need and locked the door.
Time for a cocktail! :D

I'm glad you got it working; :)
L8'er M8;
.....Willy

jwindon
01-08-2004, 10:04 PM
:thumbsup: :D [B]AWESOME!

Have one for me too. I have to get dressed and go into the office now (4PM) :rolleyes: and show my face.

Have a good one!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum