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

    显示结果:

  • 相关阅读:
    使用STM32驱动双通道12位DAC(TLV5618)
    CentOS 7挂载离线yum源
    有关于Git的使用的一点心得和说明
    STM32单片机学习心得——MDK使用技巧
    小米手机连接ADB
    我看操作系统的发展
    centos7下cups + samba共打印服务
    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤
    centos7 更新yum源
    CentOS7 安装Odoo9.0
  • 原文地址:https://www.cnblogs.com/zhbit/p/4261712.html
Copyright © 2020-2023  润新知