#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    May 2006
    Location
    Brighton, UK
    Posts
    21
    Rep Power
    0

    Compare Validator for a Checkbox


    Hi

    I'm OK with most validators, but I can't get one working for a checkbox. I need to ensure a checkbox is ticked before I move to another page.

    I'm using:

    <asp:CompareValidator ControlToValidate="cbTerms" Text="*" ControlToCompare="cbCompare" Operator="Equal" /></td>

    ... as I can only think to compare the value to one I already have (that is invisible and only used for this compare).

    It doesn't work!

    Any suggestions would be appreciated...
  2. #2
  3. Kingpin contributor
    ASP Good Citizen (1000 - 1499 posts)

    Join Date
    Nov 2005
    Location
    P.E, RSA
    Posts
    1,051
    Rep Power
    207
    Originally Posted by Bazzer747
    Hi

    I'm OK with most validators, but I can't get one working for a checkbox. I need to ensure a checkbox is ticked before I move to another page.

    I'm using:

    <asp:CompareValidator ControlToValidate="cbTerms" Text="*" ControlToCompare="cbCompare" Operator="Equal" /></td>

    ... as I can only think to compare the value to one I already have (that is invisible and only used for this compare).

    It doesn't work!

    Any suggestions would be appreciated...

    HTML Code:
    <asp:CheckBox runat="server" id="chkbox"></asp:CheckBox> 
     
    <asp:CustomValidator runat="server" ErrorMessage="Error!" OnServerValidate="ServerValidation" ClientValidationFunction="ClientValidation" 
     
              ID="Customvalidator1" /> 
     
    <asp:Button runat="server" text="submit" ID="Button1" />


    JavaScript Code:
    <script language="javascript"> 
     
    function ClientValidation(source, args) 
     
    { 
     
         args.IsValid = document.all["chkbox"].checked; 
     
    } 
     
    </script>


    VB.Net Code:
    <script runat="server" language="vb"> 
     
    sub ServerValidation( source as object, args as ServerValidateEventArgs ) 
     
         args.IsValid = chkbox.Checked 
     
    end sub 
     
    </script>


    C# Code:
    <script runat="server" language="cs"> 
     
    void ServerValidation(object source, ServerValidateEventArgs args) 
     
    { 
     
         args.IsValid = chkbox.Checked; 
     
    } 
     
    </script>


    Jo.

Similar Threads

  1. How can I find out which checkbox has been ticked?
    By matth76 in forum ASP Development
    Replies: 2
    Last Post: March 15th, 2006, 06:44 PM
  2. Update DB with Checkbox
    By kmott_1 in forum ASP Development
    Replies: 4
    Last Post: May 2nd, 2005, 03:00 PM
  3. Replies: 0
    Last Post: July 18th, 2004, 09:24 PM
  4. Passing checkbox variables to multiple pages
    By gillett2 in forum ASP Development
    Replies: 1
    Last Post: May 14th, 2004, 06:08 PM

IMN logo majestic logo threadwatch logo seochat tools logo