[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

checkbox validation with javascript

Posted on 2006-04-25
3
Medium Priority
?
2,097 Views
Last Modified: 2011-10-03
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>
0
Comment
Question by:fwsteal
3 Comments
 
LVL 2

Expert Comment

by:gordonkennedy
ID: 16534682
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.
0
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 total points
ID: 16535172
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.
0
 

Author Comment

by:fwsteal
ID: 16535391
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>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question