problem using javascript to validate an asp checkbox server control

B

Bill

The CheckAgreement() function below validates an asp checkbox server
control. the function executes, but always displays the alert and
returns false, regardless of whether the checkbox is checked or
unchecked.

Why does this happen?

function CheckAgreement()
{
var agree = document.all.agree.value;

if ( !agree.checked ) {
alert("You must check the Non-Disclosure Agreement checkbox");
return false;
}
return true;
}

Here is how the checkbox control is defined:

<asp:CheckBox ID="agree" runat="server" TextAlign="Left" Text="I
agree" />

Here is how the submit button for the form is defined:

<asp:Button ID="download" runat="server" Text="Download"
OnClick="DownLoad" UseSubmitBehavior="true" />
 
E

Elegie

Bill wrote :

Hello,
The CheckAgreement() function below validates an asp checkbox server
control. the function executes, but always displays the alert and
returns false, regardless of whether the checkbox is checked or
unchecked.

var agree = document.all.agree.value;

Here you get a string value...
if ( !agree.checked ) {

.... on which you test the "checked" property. What you actually want is
to test the "checked" property for the checkbox object, not its value,
so give a try to:

var agree = document.all.agree ;

Also, you should prefer the standard document.getElementById to the old
and non-standard document.all.


Regards,
Elegie.
 
J

Jeremy J Starcher

Bill wrote :

Also, you should prefer the standard document.getElementById to the old
and non-standard document.all.

Seconded.

This is more than just a matter of style. "document.all" does not exist
in many browsers and is regarded as IE specific.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top