use required field validator for checkbox or checkboxlist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wdwtest
    New Member
    • Nov 2011
    • 1

    use required field validator for checkbox or checkboxlist

    Code:
    <asp:CheckBox ID="CheckBox1" runat="server" />
        
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
            ControlToValidate="CheckBoxList1" runat="server" 
            ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    i have used this for checkbox

    it gives error as

    Control 'CheckBoxList1' referenced by the ControlToValida te property of 'RequiredFieldV alidator2' cannot be validated.

    how to use required field validator for checkbox or checkboxlist
  • Palyadav
    New Member
    • Dec 2011
    • 23

    #2
    There are no fields in the check box list to fill , So RequiredFiledVa lidator doesn't work for that control.

    You have to use "CustomValidato r" for CheckBoxList... Check this code..

    Code:
    function checkTemp(sender,args)
    {
         for(var i=0;i<=document.getElementById("CheckBoxList1").firstChild.children.length-1;i++)
         {
              if(document.getElementById("CheckBoxList1").firstChild.children(i).cells(0).children(0).checked)=='true'
              alert('One is checked')
              args.IsValid=true
              return
                        
         }
         alert('None is selected')
         args.IsValid=false
    }
    
    
    <asp:CustomValidator id="valcheckBoxTemp" runat="server" ClientValidationFunction="checkTemp" ErrorMessage="This is required control "></asp:CustomValidator>

    Comment

    Working...