0
<tr>
    <td>
        <asp:CheckBox ID="checkdoc" runat="server" Checked="false" />:Document
    </td>
    <td>
        <asp:CheckBox ID="checktwocheque" runat="server" Checked="false" />:Two Cheques
    </td>
    <td>
        <asp:CheckBox ID="checkIdprf" runat="server" Checked="false" />:ID Proof
    </td>
</tr>
<tr>
    <td>
        <asp:CheckBox ID="checkpancrd" runat="server" Checked="false" />:PAN Card
    </td>
    <td>
        <asp:CheckBox ID="checkAddrssprf" runat="server" Checked="false" />:Address Proof
    </td>
</tr>
<tr>
    <td colspan="4" align="center">
        <asp:Button ID="btnfarmrecordsave" runat="server" Text="Save" OnClientClick="return Validations();" OnClick="btnfarmrecordsave_Click" />
    </td>
</tr>

I have written code like this for choosing at least one checkbox. How can I write code for validation in javascript?. I want to display one message "Please select at least one checkbox" if user hasn't selected any checkbox?

Damon
  • 3,004
  • 7
  • 24
  • 28
santhosha
  • 377
  • 1
  • 6
  • 28
  • why not check it on the server side when they do a postback on the button click? (just curious to know, that's all). Otherwise you will have to do an onclick event, call a function which then checks to see if the checkbox (found by ID) isChecked – Ahmed ilyas Dec 09 '13 at 09:36
  • may be this will help you...http://stackoverflow.com/questions/1228112/how-do-i-make-a-checkbox-required-on-an-asp-net-form – yashhy Dec 09 '13 at 09:41
  • http://stackoverflow.com/questions/9709209/html-select-only-one-checkbox-in-a-group – Sachin Dec 09 '13 at 09:41

4 Answers4

2

Please try to implement the Validations function like this:

<script type="text/javascript">
    function Validations() {
        if (!(document.getElementById("<%=checkdoc.ClientID%>").checked ||
            document.getElementById("<%=checktwocheque.ClientID%>").checked ||
            document.getElementById("<%=checktwocheque.ClientID%>").checked ||
            document.getElementById("<%=checkIdprf.ClientID%>").checked ||
            document.getElementById("<%=checkpancrd.ClientID%>").checked ||
            document.getElementById("<%=checkAddrssprf.ClientID%>").checked)) {

            alert('You have to select atleast one choice!');
            return false;
        } else {
            return true;
        }
    }
</script>
Alice
  • 1,255
  • 1
  • 9
  • 7
0
    <style>
    .abc{
display:box
    //any style you want to put
    }
    </style>
    <script>
    function Validations()
    {
    if ($('input.abc').not(':checked').length > 0)
    {
    alert("please select atleast one checkbox");
    }
    }
    </script>

 <tr>
    <td>
        <asp:CheckBox ID="checkdoc" runat="server" Checked="false" class="abc"/>:Document
    </td>
    <td>
        <asp:CheckBox ID="checktwocheque" runat="server" Checked="false" class="abc"/>:Two Cheques
    </td>
    <td>
        <asp:CheckBox ID="checkIdprf" runat="server" Checked="false" class="abc"/>:ID Proof
    </td>
</tr>
<tr>
    <td>
        <asp:CheckBox ID="checkpancrd" runat="server" Checked="false" class="abc"/>:PAN Card
    </td>
    <td>
        <asp:CheckBox ID="checkAddrssprf" runat="server" Checked="false" class="abc"/>:Address Proof
    </td>
</tr>
<tr>
    <td colspan="4" align="center">
        <asp:Button ID="btnfarmrecordsave" runat="server" Text="Save" OnClientClick="return Validations();"
            OnClick="btnfarmrecordsave_Click" />
    </td>
</tr>
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25
0

Javascript code :

    if (!$(':checkbox').is(':checked')) {
        alert('please, select ...');
    }
user3041160
  • 684
  • 3
  • 5
0
function Validations()
{
  var chk = document.getElementsByTagName('input');
  for (var i = 0; i < chk.length; i++)
  {
    if (chk[i].type == 'checkbox')
    {
       if (chk[i].checked) {return true}
    }
  }
  alert("Please select atleast one checkbox");
  return false;
}
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133