For Loop not working??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ercwebdev@yahoo.co.uk

    For Loop not working??

    Can anyone tell me why this For loop isn't working (see below)
    The error message is says:

    Error: 'document.email Form.FNAME_CHIL D' is null or not an object

    Thanks in advance,
    Alan R



    // Loop to check if first name field is populated - send info to
    childnamecheck function
    for(i = 1; i < 6; i++)
    {
    if (document.email Form.FNAME_CHIL D[i].value !='')
    {
    childnamecheck( i)
    }
    }
    if (Ok == false)
    {
    missinginfo += "\n - There is information missing from your Child
    details ";
    }

  • ASM

    #2
    Re: For Loop not working??

    ercwebdev@yahoo .co.uk wrote:[color=blue]
    > Can anyone tell me why this For loop isn't working (see below)
    > The error message is says:
    >
    > Error: 'document.email Form.FNAME_CHIL D' is null or not an object
    >
    > Thanks in advance,
    > Alan R
    >
    >
    >
    > // Loop to check if first name field is populated - send info to
    > childnamecheck function
    > for(i = 1; i < 6; i++)[/color]

    you have only 6 elements in your form 'emailForm' ?
    'emailForm' is it the *name* of your form ?

    each element in this form has same name 'FNAME_CHILD' ?
    [color=blue]
    > {
    > if (document.email Form.FNAME_CHIL D[i].value !='')
    > {
    > childnamecheck( i)
    > }
    > }
    > if (Ok == false)
    > {
    > missinginfo += "\n - There is information missing from your Child
    > details ";
    > }
    >[/color]


    --
    Stephane Moriaux et son [moins] vieux Mac

    Comment

    • Lee

      #3
      Re: For Loop not working??

      ercwebdev@yahoo .co.uk said:[color=blue]
      >
      >Can anyone tell me why this For loop isn't working (see below)
      >The error message is says:
      >
      >Error: 'document.email Form.FNAME_CHIL D' is null or not an object
      >
      >Thanks in advance,
      >Alan R
      >
      >
      >
      >// Loop to check if first name field is populated - send info to
      >childnameche ck function
      >for(i = 1; i < 6; i++)
      >{
      >if (document.email Form.FNAME_CHIL D[i].value !='')[/color]

      You haven't given us enough information to do anything but guess.
      How are we supposed to know if FNAME_CHILD is correct or not?
      It does seem odd that your loop begins with 1. Arrays in Javascript
      begin with element zero.

      Comment

      • ercwebdev@yahoo.co.uk

        #4
        Re: For Loop not working??

        Hi,
        Thanks for advice. Here is the code below. It relates to 5 'objects'
        in the form, i.e .child's detials:

        <SCRIPT LANGUAGE="JAVAS CRIPT">
        <!--



        // Check drop down menu - age field and return message
        function age(childid)
        {
        if (childid.select edIndex >= 13)
        alert('Note- Applications for children over 16 years by 30th September
        cannot be processed until the child has returned to school after the
        summer holidays. Please complete and submit a separate form for these
        children.');
        }

        //Check top half of form submission for missing fields
        function checkFields()
        {
        missinginfo = "";
        if (document.email Form.TITLE.valu e == "--") {
        missinginfo += "\n - Title";
        }
        if (document.email Form.FNAME.valu e == "") {
        missinginfo += "\n - First Name";
        }
        if (document.email Form.LNAME.valu e == "") {
        missinginfo += "\n - Surname";
        }
        if (document.email Form.ADDRESS.va lue == "") {
        missinginfo += "\n - Address";
        }
        if (document.email Form.POSTCODE.v alue == "") {
        missinginfo += "\n - Postcode";
        }
        if (document.email Form.NI_NUMBER. value == "") {
        missinginfo += "\n - National Insurance Number";
        }
        if((document.em ailForm.IS_JSA. value == "NO") &&
        (document.email Form.HOUSING_CT .value == "NO"))
        {
        missinginfo += "\n - You have not selected any benefits";
        }
        if (document.email Form.DEC_YES.va lue != "Yes") {
        missinginfo += "\n - You have not agreed to the declaration";
        }


        // Loop to check if first name field is populated - send info to
        childnamecheck function
        //for(i = 1; i < 6; i++)
        //{
        //if (document.email Form.FNAME_CHIL D[i].value !='')
        // {
        // childnamecheck( i)
        // }
        //}
        //if (Ok == false)
        // {
        // missinginfo += "\n - There is information missing from your
        Child details ";
        // }

        var Ok = new Boolean;
        Ok = true;

        for(i = 1; i < 6; i++) {
        if (!/\S/.test (document.email Form['FNAME_CHILD' +i].value))
        childnamecheck( i)
        }
        if (!Ok) missinginfo += '\n - There is information missing from your
        Child details ';


        // final output message
        if (missinginfo != "") {
        missinginfo ="_____________ _______________ _\n" +
        "You failed to correctly fill in your:\n" +
        missinginfo + "\n____________ _______________ __" +
        "\nPlease re-enter and submit again!";
        alert(missingin fo);
        return false;
        }
        else return true;
        }




        // Function to check the rest of the fields
        function childnamecheck( num)
        {
        if(document.ema ilForm['FNAME_CHILD' +num].value =='')
        {
        Ok = false;
        }
        if(document.ema ilForm['LNAME_CHILD' +num].value =='')
        {
        Ok = false;
        }
        if(document.ema ilForm['DAY_CHILD' +num].value =='--')
        {
        Ok = false;
        }
        if(document.ema ilForm['MONTH_CHILD' +num].value =='---')
        {
        Ok = false;
        }
        if(document.ema ilForm['YEAR_CHILD' +num].value =='----')
        {
        Ok = false;
        }
        if(document.ema ilForm['AGE_CHILD' +num].value =='--')
        {
        Ok = false;
        }
        if(document.ema ilForm['SEX_CHILD' +num].value =='-')
        {
        Ok = false;
        }
        if(document.ema ilForm['SCHOOL_CHILD' +num].value =='select:')
        {
        Ok = false;
        }
        }

        </SCRIPT>

        Comment

        • Zoe Brown

          #5
          Re: For Loop not working??

          [color=blue]
          > // Loop to check if first name field is populated - send info to
          > childnamecheck function
          > //for(i = 1; i < 6; i++)[/color]

          should be

          for(i = 0; i < 5; i++)


          Comment

          • ercwebdev@yahoo.co.uk

            #6
            Re: For Loop not working??

            shoot, I changed that - doesn't have any effect. It runs now, but just
            comes out that everything is true, even when there are errors!

            Comment

            • Lee

              #7
              Re: For Loop not working??

              ercwebdev@yahoo .co.uk said:[color=blue]
              >
              >Hi,
              >Thanks for advice. Here is the code below. It relates to 5 'objects'
              >in the form, i.e .child's detials:[/color]

              What I meant was that without seeing the HTML that defines the
              fields that you're accessing, we can only guess why you're
              having trouble accessing them.

              Try creating an example by trimming your actual page down to the
              smallest subset that demonstrates the problem. Often you will
              spot the problem yourself in the process. If not, post the example
              and we should have all the information we need to find the problem.

              Comment

              • Lee

                #8
                Re: For Loop not working??

                ercwebdev@yahoo .co.uk said:[color=blue]
                >
                >shoot, I changed that - doesn't have any effect. It runs now, but just
                >comes out that everything is true, even when there are errors![/color]

                You've declared your variable "Ok" to be local to function checkFields(),
                but seem to try to set its value in function childnamecheck( )

                Also note that the lines:

                var Ok = new Boolean;
                Ok = true;

                - Create a local variable named Ok.
                - Creates a new Boolean object and assigns it to Ok
                - Then destroys that object, replacing it with a primitive boolean
                value of true.

                I would replace them with:

                var Ok = true;

                and then have childnamecheck( ) return true or false, rather than
                assign a value to Ok, and call it as:

                Ok &= childnamecheck( i);

                The &= operator assigns to Ok the logical AND of the returned
                value with the current value of Ok, so that Ok doesn't simply
                reflect whether or not the last value checked is valid.

                Comment

                • ercwebdev@yahoo.co.uk

                  #9
                  Re: For Loop not working??

                  Thanks a lot, that got it working. Your a star!
                  Ta again.

                  Cheers,
                  Alan R

                  Comment

                  Working...