首页 > 代码库 > ASP:Checkbox验证非空的一种方法

ASP:Checkbox验证非空的一种方法

       有的时候填表单必须非空,那checkbox比较麻烦,不能用RequiredFieldValidator 控件,所以我就用JS和customvalidator控件结合来实现。

JS代码:

 <script language="javascript" type="text/javascript">        //验证checkbox不空        function checkIsNotnull(source, args) {            var ch1 = document.getElementById(‘<%= type1.ClientID %>‘);            var ch2 = document.getElementById(‘<%= type2.ClientID %>‘);            var ch3 = document.getElementById(‘<%= type3.ClientID %>‘);            var ch4 = document.getElementById(‘<%= type4.ClientID %>‘);            var ch5 = document.getElementById(‘<%= type5.ClientID %>‘);            var ch6 = document.getElementById(‘<%= type6.ClientID %>‘);        if (ch1.checked) {            args.IsValid = true;            return;        }        if (ch2.checked) {            args.IsValid = true;            return;        }        if (ch3.checked) {            args.IsValid = true;            return;        }        if (ch4.checked) {            args.IsValid = true;            return;        }        if (ch5.checked) {            args.IsValid = true;            return;        }        if (ch6.checked) {            args.IsValid = true;            return;        }        args.IsValid = false;}

  aspx代码:

<td colspan="2">                                     <asp:CheckBox runat="server" ID ="type1" Font-Size="Large"  Text ="游戏影音本" />                                     &nbsp;&nbsp;&nbsp;                                     <asp:CheckBox runat="server" ID ="type2" Font-Size="Large"  Text ="家庭娱乐本" />                                     &nbsp;&nbsp;&nbsp;                                     <asp:CheckBox runat="server" ID ="type3" Font-Size="Large"  Text ="高清影音本" />                                     &nbsp;&nbsp;&nbsp;                                     <asp:CheckBox runat="server" ID ="type4" Font-Size="Large"  Text ="商务办公本" />                                     &nbsp;&nbsp;&nbsp;                                     <asp:CheckBox runat="server" ID ="type5" Font-Size="Large"  Text ="全能学生本" />                                     &nbsp;&nbsp;&nbsp;                                     <asp:CheckBox runat="server" ID ="type6" Font-Size="Large"  Text ="轻薄便携本" />                                    <asp:customvalidator id="CustomValidator1" runat="server" ForeColor="Red" ErrorMessage="不能为空!" ClientValidationFunction="checkIsNotnull"></asp:customvalidator> </td>

显示结果:

技术分享

ASP:Checkbox验证非空的一种方法