Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Valdating Check boxes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bobby - 19 Sep 2007 19:56 GMT
I'm having a problem which I thought would be a very simple thing but is
> > turning out to be a difficult challenge. I have a web form with several input
> > fields and I have a single checkbox, not a checkbox list. Before the form can
> > be submitted I want to require that the checkbox be checked. Sounds simple
> > but non of the validation controls seem to work correct for this, or maybe
> > I'm just not using them correctly. I have done several searches but I can't
> > seem to find any examples of this type of thing being done. Which is very
> > surprising because this sort of checkbox is a very common thing. i.e. "I
> > agree to the terms....." Can anyone point me to an example? I'm using ASP.NET 2.0
Mark Rae [MVP] - 19 Sep 2007 20:43 GMT
> Can anyone point me to an example?

<script type="text/javascript">
   function validateForm()
   {
       if (!document.getElementById('<%=MyCheckBox.ClientID%>').checked)
       {
           alert ('You must agree to the terms');
           document.getElementById('<%=MyCheckBox.ClientID%>').focus();
           return false;
       }
   }
</script>

<form id="form1" runat="server">
   <asp:CheckBox ID="MyCheckBox" runat="server" />
   <asp:Button ID="MyButton" Text="Submit" OnClick="MyButton_Click"
OnClientClick="return validateForm();" />
</form>

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

bruce barker - 19 Sep 2007 20:57 GMT
pretty trival:

<script type="text/javascript">
function ackCheck(s,a)
{
   a.IsValid = document.getElementById('<%=chkAck.ClientID%>').checked;
   return a.IsValid;
}
</script>

<asp:CustomValidator runat=server
    ControlToValidate="chkAck"
    ErrorMessage="Please Acknowledge"
    ClientValidationFunction="ackCheck"
    Display="Dynamic"
/>
<asp:checkbox runat=server id="chkAck"
    Text="Acknowledge" />

-- bruce (sqlwork.com)

> I'm having a problem which I thought would be a very simple thing but is
>>> turning out to be a difficult challenge. I have a web form with several input
[quoted text clipped - 5 lines]
>>> surprising because this sort of checkbox is a very common thing. i.e. "I
>>> agree to the terms....." Can anyone point me to an example? I'm using ASP.NET 2.0

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.