Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nesting if statements

    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.
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #4
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.");
    }

    }
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please try this:
    Code:
    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

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Nope

    Should I send the page?

    Can't get past the first edit. The District check works.
    Last edited by jwindon; 01-08-2004 at 05:10 AM.
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can attach it to your post.
    If it is too large or has several files,
    Try zipping before attaching.

    .....Willy

  • #8
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Attached Files Attached Files
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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.

    .....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.
    Attached Files Attached Files
    Last edited by Willy Duitt; 01-08-2004 at 11:04 AM.

  • #10
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Willy! Your work looks great to me!

    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?
    Attached Files Attached Files
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #11
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #12
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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




    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;
    }
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #13
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is NULL?

    Thanks to Willy's guiding code and some serious combing and testing, the page is working properly!!!!!!11


    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;
    }
    -- Always thankful for kind and patient teachers willing to help a beginner.

  • #14
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try replacing the statement with this:
    Code:
    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.

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

    .....Willy

  • #15
    New Coder
    Join Date
    Jan 2004
    Location
    Kentucky
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    -- Always thankful for kind and patient teachers willing to help a beginner.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •