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?

3
  • 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 Commented Dec 9, 2013 at 9:36
  • may be this will help you...stackoverflow.com/questions/1228112/… Commented Dec 9, 2013 at 9:41
  • stackoverflow.com/questions/9709209/… Commented Dec 9, 2013 at 9:41

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

Comments

0

Javascript code :

    if (!$(':checkbox').is(':checked')) {
        alert('please, select ...');
    }

Comments

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>

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.