Checkbox Validation Works on FF but not IE

M

martialtiger

I'm having a hard time trying to figure out why the following
validation works on FF and not IE versio 6.0

function checkForm()
{
var errmsg = "Please correct all errors before continuing:
\n\n";
var errors=0;
//var formElement = document.vpform;
var agree = document.vpform.agree.checked;
//formElement["agree"].checked;
var bundleID = document.vpform.bundleID.value;
//formElement["bundleID"].value;

if (agree != false)
{
if (bundleID != 0) {
return true;
}
else
{
alert("You must select a lesson you wish to
receive.");
return false;
}
}
else
{
alert("You must select the checkbox to agree with the
terms.");
return false;
}
}
-->

You'll notice I commented out what I thought were the problems.

TIA!
 
R

RobG

martialtiger said:
I'm having a hard time trying to figure out why the following
validation works on FF and not IE versio 6.0

But you don't specify what 'works' is, or even what not working is.

function checkForm()
{
var errmsg = "Please correct all errors before continuing:
\n\n";

Don't allow posted code to auto-wrap, manually wrap it at about 70
characters. The preferred indent is 2 or 4 spaces.

var errors=0;
//var formElement = document.vpform;

If there is a form in the document with a name of 'vpform', then
formElement will be a reference to it. If there is more than one form
with that name (names don't need to be unique) then formElement will be
a collection.

var agree = document.vpform.agree.checked;

agree may now be:

1. a boolean, either -true- if the element exists, has a
checked property and it's checked or

2. -false- if it's not

3. if the element doesn't exist, an error will result - you
can't access properties of non-existent objects

4. if the element does exist but doesn't have a checked property,
agree will be 'undefined', which will type-convert to 'false'
in an -if- test.

IE adds most names and all IDs as global variables that reference the
element they belong too. In the case of duplicate names, only the first
is added; form controls with names and ids aren't added though the same
element outside a form (i.e. when not a form control) are.

As a result, creating a local variable with the same name as an element
can lead to confusion so change the local variable 'agree' to maybe
'b_agree' to show it's a boolean.

//formElement["agree"].checked;

That simply returns the value of the checked property, if you want to
set it to something (say make it checked):

formElement["agree"].checked = true;

var bundleID = document.vpform.bundleID.value;
//formElement["bundleID"].value;

As above... Also again you've used a local variable with the same name
as a form element (bundleID), change that.

if (agree != false)

There is no need to test against -false-, you can just do:

if (agree){

{
if (bundleID != 0) {
return true;

Do you really mean the number zero? Or do you mean "" (empty string)?

}
else
{
alert("You must select a lesson you wish to
receive.");
return false;
}
}
else
{
alert("You must select the checkbox to agree with the
terms.");

This makes me think you need:

var o;
if ( (o=document.forms['vpform'])
&& (o=o.elements['agree'])
&& o.checked )
{
// agree exists and has been checked
}
else
{
// either:
// this browser doesn't support the forms collection or
// there isn't a form named 'vpform' or
// it doesn't have an element named 'agree' or
// 'agree' doesn't have a checked property or
// 'agree' isn't checked
// Deal with it...
}

The quick 'n dirty way is:

if (!document.vpform.agree.checked){
// it's not checked, deal with it.
}

[...]

Why is '-->' there? Don't use HTML comments anywhere inside script
elements. Once upon a time, it was thought necessary to 'hide' scripts
with an opening <!-- and a closing //-->, but that's not been necessary
since Netscape 2/IE 3 (about 10 years ago).

You'll notice I commented out what I thought were the problems.

Much better to post a minimal 'working' example that shows the error,
explain what actually happens (i.e. the error) and what you want to
happen. The more effort you put into posting the question, the faster
you'll get answers (and likely more of them).
 
T

Thomas 'PointedEars' Lahn

martialtiger said:
Thanks Rob for the response. I found the problem and appreciate
the tips.

But you did not say what the problem was, for the second time now.
Usenet does not work this way.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 22 May
2006 23:20:26 remote, seen in Thomas
'PointedEars' Lahn said:
But you did not say what the problem was, for the second time now.
Usenet does not work this way.

He was not addressing you, O socially retarded one; and the article to
which you respond is getting on for a week old.

When, if ever, do you propose to stop behaving like a six-year-old?

Please read FYI28/RFC1855, and keep doing so until you understand how
News should be used, or until you have been cured by a paediatric
psychotherapist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top