470,648 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

For Loop not working??

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

Error: 'document.emailForm.FNAME_CHILD' 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.emailForm.FNAME_CHILD[i].value !='')
{
childnamecheck(i)
}
}
if (Ok == false)
{
missinginfo += "\n - There is information missing from your Child
details ";
}

Sep 16 '05 #1
8 1595
ASM
er*******@yahoo.co.uk wrote:
Can anyone tell me why this For loop isn't working (see below)
The error message is says:

Error: 'document.emailForm.FNAME_CHILD' 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++)
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' ?
{
if (document.emailForm.FNAME_CHILD[i].value !='')
{
childnamecheck(i)
}
}
if (Ok == false)
{
missinginfo += "\n - There is information missing from your Child
details ";
}

--
Stephane Moriaux et son [moins] vieux Mac
Sep 16 '05 #2
Lee
er*******@yahoo.co.uk said:

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

Error: 'document.emailForm.FNAME_CHILD' 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.emailForm.FNAME_CHILD[i].value !='')


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.

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

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

// Check drop down menu - age field and return message
function age(childid)
{
if (childid.selectedIndex >= 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.emailForm.TITLE.value == "--") {
missinginfo += "\n - Title";
}
if (document.emailForm.FNAME.value == "") {
missinginfo += "\n - First Name";
}
if (document.emailForm.LNAME.value == "") {
missinginfo += "\n - Surname";
}
if (document.emailForm.ADDRESS.value == "") {
missinginfo += "\n - Address";
}
if (document.emailForm.POSTCODE.value == "") {
missinginfo += "\n - Postcode";
}
if (document.emailForm.NI_NUMBER.value == "") {
missinginfo += "\n - National Insurance Number";
}
if((document.emailForm.IS_JSA.value == "NO") &&
(document.emailForm.HOUSING_CT.value == "NO"))
{
missinginfo += "\n - You have not selected any benefits";
}
if (document.emailForm.DEC_YES.value != "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.emailForm.FNAME_CHILD[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.emailForm['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(missinginfo);
return false;
}
else return true;
}


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

</SCRIPT>

Sep 16 '05 #4
// Loop to check if first name field is populated - send info to
childnamecheck function
//for(i = 1; i < 6; i++)


should be

for(i = 0; i < 5; i++)
Sep 16 '05 #5
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!

Sep 16 '05 #6
Lee
er*******@yahoo.co.uk said:

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


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.

Sep 16 '05 #7
Lee
er*******@yahoo.co.uk said:

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!


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.

Sep 16 '05 #8
Thanks a lot, that got it working. Your a star!
Ta again.

Cheers,
Alan R

Sep 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hardrock | last post: by
5 posts views Thread by L. Oborne | last post: by
34 posts views Thread by Frederick Gotham | last post: by
6 posts views Thread by uche | last post: by
4 posts views Thread by joaotsetsemoita | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.