Wednesday 7 July, 2010

CheckBoxList Required Validation in Javascript

  1. <asp:CheckBoxList ID="chkList" runat="server">
  2. <asp:ListItem>Item Oneasp:ListItem>
  3. <asp:ListItem>Item twoasp:ListItem>
  4. <asp:ListItem>Item threeasp:ListItem>
  5. <asp:ListItem>Item fourasp:ListItem>
  6. asp:CheckBoxList>
  7. <asp:Button ID="btnSubmit" runat="server" ValidationGroup="CHK" OnClientClick="return validateChk();" Text="Validate"/>

JAVASCRIPT CODE:
  1. function validateChk() {
  2. debugger;
  3. var found = false;
  4. var validated = false;
  5. var id = document.getElementById("").id;
  6. var elements = document.forms[0].elements;
  7. for (i = 0; i <>
  8. if (elements[i].type == "checkbox" && elements[i].id.indexOf(id) > -1) {
  9. found = true;
  10. if (elements[i].checked) {
  11. validated = true;
  12. break;
  13. }
  14. }
  15. }
  16. if (found && validated)
  17. return true;
  18. else
  19. return false;
  20. }

No comments:

Post a Comment