Checkbox Validation - Best Solution

Rate It (3)

Last post 09-06-2009 6:15 AM by Ankit Jain. 11 replies.

Sort Posts:

  • Checkbox Validation - Best Solution

    12-13-2006, 3:07 PM
    • Member
      749 point Member
    • DannyDep
    • Member since 10-18-2005, 11:40 PM
    • Posts 316

    I've spent a couple of hours searching for a solution to validating a checkbox, since the RequiredFieldValidator does not work with Checkboxes.

    Here is a great solution. 

    Look for  "skmValidators - custom validation controls for CheckBoxes and CheckBoxLists for ASP.NET 2.0."

    http://scottonwriting.net/sowBlog/CodeProjects.htm  

    Thanks Scott.

  • Re: Checkbox Validation - Best Solution

    12-16-2006, 11:52 AM
    • All-Star
      17,438 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474
    • TrustedFriends-MVPs
    Actually I like to use AJAX for Checkbox validation, if you add the checkboxes inside an UpdatePanel and then on the server side you can check if they are check or not and update a red label by the checkbox. To the user, looks like works the same way the validation, for you, is just a little bit more magic to write.
    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: Checkbox Validation - Best Solution

    12-18-2006, 2:01 AM
    • Contributor
      4,219 point Contributor
    • moredotnet
    • Member since 11-24-2006, 12:07 PM
    • Millenium City
    • Posts 780

    Hi 

    If your just validating that checkbox is checked use this javascript snippet

    document.getElementById("cb").checked == true

     If you want to check & uncheck all checkboxes at one go...use this

    function check(field)
    {
     if (checkflag == "false")
     {
      for (i = 0; i < field.length; i++)
      {
       if(field[i].disabled == false)
       field[i].checked = true;
      }
      checkflag = "true";
      return "Uncheck All";
     }
    else
    {
    for (i = 0; i < field.length; i++)
     {
       if(field[i].disabled == false)
       field[i].checked = false;
     }
     checkflag = "false";
     return "Check All";
     }
    }

     

    Cheers

    moredotnet

    http://moredotnet.googlepages.com

     

    Vishal Khanna



    ASP.NET|C#|AJAX|SQL|Design Patterns|Interview FAQs
  • Re: Checkbox Validation - Best Solution

    12-20-2006, 11:09 AM
    • Contributor
      4,100 point Contributor
    • Scott Mitchell
    • Member since 06-15-2002, 8:41 PM
    • San Diego, CA
    • Posts 707
    • ASPInsiders
      TrustedFriends-MVPs
    DannyDep:

    I've spent a couple of hours searching for a solution to validating a checkbox, since the RequiredFieldValidator does not work with Checkboxes.

    Here is a great solution. 

    Look for  "skmValidators - custom validation controls for CheckBoxes and CheckBoxLists for ASP.NET 2.0."

    http://scottonwriting.net/sowBlog/CodeProjects.htm  

    Thanks Scott.

    You're welcome, DannyDep. Let me also point you (and others) to an accompanying article, which dissects and discusses the custom, compiled server control (skmValidators):

    Creating Validator Controls for the CheckBox and CheckBoxList

    Happy Programming!


    -- Scott Mitchell
    -- mitchell@4guysfromrolla.com
    -- http://scottonwriting.net/sowblog/
    -- http://www.4GuysFromRolla.com/ScottMitchell.shtml
    Filed under:
  • Re: Checkbox Validation - Best Solution

    03-12-2009, 3:24 AM
    • Participant
      1,367 point Participant
    • sunilyadav165
    • Member since 02-13-2009, 5:56 AM
    • Mumbai
    • Posts 254

     Hi,
      Here is the code using javascript....

    <html>
    <head>
    <title> Checkbox validations</title>


    <SCRIPT TYPE="text/javascript">

    function CheckBoxesValidations() {
        if (document.form1.CHKBOX1.checked == false &&
            document.form1.CHKBOX2.checked == false &&
            document.form1.CHKBOX3.checked == false)
            {
            alert (' you have not  choose any of the checkboxes!');
            return false;
            }
        else
            {
            return true;
            }
        }

    </SCRIPT>

    </head>
    <body>
    <form onsubmit="return CheckBoxesValidations() ;" name="form1" action="">
    <input type="checkbox" name="CHKBOX1" value="1">1</p>
    <input type="checkbox" name="CHKBOX2" value="2">2</p>
    <input type="checkbox" name="CHKBOX3" value="3">3</p>
    <input type="submit" value="Submit!" />
    </form>
    </body>
    </html>

    If you like my post mark it as ANSWER.

    Regards,
    sunil

  • Re: Checkbox Validation - Best Solution

    05-05-2009, 3:12 AM
    • Member
      55 point Member
    • ajith P T
    • Member since 09-09-2008, 11:20 AM
    • Posts 48

     

    You can use Customevalidator for validating the checkbox

     

    Ajith P.T
    Manager Technology
    Sofker Solutions Pvt. Ltd.
    Cochin
    kerala, India
  • Re: Checkbox Validation - Best Solution

    05-05-2009, 7:28 AM
    • Member
      4 point Member
    • nalinbc
    • Member since 05-02-2009, 1:13 PM
    • Ottawa, ON
    • Posts 3

    Here's a solution that I used in a recent website: 

    Since the asp:CustomValidator does not accept an asp:CheckBox as the control to validate, the first step is to set it to any other control in the page (e.g. a textbox).

    The second step is to write a bit of JavaScript (JQuery) to return the non-checked state of the checkbox to validate. Below, I am showing a JavaScript variable being set, that is for use by the JavaScript method.

    <script type="text/javascript">

    var controlID1 = '<%= CheckBoxAgree.ClientID %>';

    </script>

    Here's the JavaScript function you'll need to write:

    function IsNotChecked1(obj, args) {

    var checkbox = $("#" + controlID1); args.IsValid = checkbox.attr('checked');

    }

    Here's the code to include in the aspx page:

    <asp:CheckBox ID="CheckBoxAgree" runat="server"

    Text="I confirm that the above information is accurate to the best of my knowledge and agree to the terms and conditions" />

     

    <asp:CustomValidator ID="CustomValidatorAgree" runat="server" ClientValidationFunction='IsNotChecked1'

    onservervalidate="CustomValidatorAgree_ServerValidate" ValidationGroup="group1" ControlToValidate="TextBoxTelephone"

    ErrorMessage="'I confirm' must be checked" ValidateEmptyText="True" ></asp:CustomValidator>

     

    Note that above we are setting the control to validate to any other control in the page.

    Since you must also validate on the server side (best practice), here's the C# code:

     

    protected void CustomValidatorAgree_ServerValidate(object source, ServerValidateEventArgs args)

    {

    args.IsValid = (CheckBoxAgree.Checked);

    }

     

    Best regards,

     - Nalin Jayasuriya

     

    Nalin J
  • Re: Checkbox Validation - Best Solution

    05-28-2009, 11:55 PM
    • Participant
      1,572 point Participant
    • Spider Master
    • Member since 10-16-2007, 5:32 PM
    • New Zealand
    • Posts 447

    I have created my own Checkbox and checkboxlist validator if your interested.

    It works just the same as all <asp:Validators and works with the validation summary also.

    You just add the dll to your toolbox and drop it on the page, set the properties and go nuts Big Smile

    "If Your Question Has Been Answered,
    Please Mark It As the Answer"
  • Re: Checkbox Validation - Best Solution

    05-30-2009, 1:51 AM
    • Star
      11,839 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,328

    You can use a compare validator for the checkbox checking the values of the checkbox.

  • server control and custom control differences

    06-11-2009, 9:13 AM
    • Member
      4 point Member
    • sunrobin23
    • Member since 06-03-2009, 6:04 AM
    • Posts 2

    what is server control and custom control diff.?

  • Re: server control and custom control differences

    06-11-2009, 4:13 PM
    • Member
      106 point Member
    • praveen.singh
    • Member since 01-05-2009, 4:03 PM
    • Chandigarh
    • Posts 19

    You can create your own control that is custom control,you can find that custom control in the form of dll like Ajax control tool kit,

    server control provided by default asp.net library you can normaly find in asp.net tool box

     

     

    Thanks

    Praveen Singh
    Software Developer
  • Re: Checkbox Validation - Best Solution

    09-06-2009, 6:15 AM
    • Member
      18 point Member
    • Ankit Jain
    • Member since 09-02-2009, 12:42 PM
    • Posts 4

     or directly we can search for htmltablecontrols and give the id of the checkbox that needs to be checked

    AJ
Page 1 of 1 (12 items)