checkbox validation with javascript

why does this not work?

<script type="text/javascript">
function validateCheckbox() {
 if(!document.form1.CheckBoxESA.checked){
    alert("required");
   return false;
  }
   return true;
 }
</script>


<form id="form1" runat="server" onSubmit="return validateCheckbox();">
<table cellpadding="0" cellspacing="0" border="0">
 <tr>
   <td>
      <asp:CheckBox id="CheckBoxESA" runat="server" TextAlign="Left" Text="I agree to the terms">
      </asp:CheckBox>
   </td>
 </tr>
 <tr>
   <td colspan="2" align="center">
     <input type="submit" name="submit" value="Submit">
   </td>
 </tr>
</table>
</form>
fwstealAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gordonkennedyCommented:
I don't think you can access asp elements from within a client script.

Try having your checkbox as an html item instead :
<INPUT type=checkbox id=CheckBoxESA>I agree to the terms<P>

Then reference this in your client script using :
if (!document.getElementById('CheckBoxESA').checked) {

Hope this helps.
DBAduck - Ben MillerPrincipal ConsultantCommented:
Because a Server side object may change it's name when it renders to the browser to avoid naming conflicts.  So in your code it will be named CheckBoxESA and if you viewed the source of your web page, it may be ctrl0_CheckBoxESA.

So when you do that you will want to make sure that you render

<script type="text/javascript">
function validateCheckbox() {
 if(!document.form1.<%= CheckBoxESA.ClientID %>.checked){
    alert("required");
   return false;
  }
   return true;
 }
</script>

This will send out the name of the object that will be rendered and not just what you expect to be rendered.
But I would use the getElementById( ) instead of just going by form1.

Ben.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fwstealAuthor Commented:
this is what i did in asp.net 2.0 c#:

<script type="text/javascript">
function validateCheckbox() {
 if(!document.form1.<%= CheckBoxESA.ClientID %>.checked){
    alert("The ESA is required.");
   return false;
  }
   return true;
 }
</script>

<asp:Button id="ButtonSubmit" runat="server" Text="Submit" OnClick="ButtonSubmit_Click" OnClientClick="return validateCheckbox();"></asp:Button>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.