如何在asp.net中对复选框进行验证?

时间:2021-03-15 03:14:43
<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?

我已经编写了这样的代码来选择至少一个复选框。如何在javascript中编写验证代码?如果用户没有选中任何复选框,我想显示一条消息“请选择至少一个复选框”?

4 个解决方案

#1


2  

Please try to implement the Validations function like this:

请尝试像这样实现Validations函数:

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

#2


0  

Javascript code :

Javascript代码:

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

#3


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>

#4


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

#1


2  

Please try to implement the Validations function like this:

请尝试像这样实现Validations函数:

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

#2


0  

Javascript code :

Javascript代码:

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

#3


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>

#4


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