ASP:Checkbox驗證非空的一種方法

來源:互聯網
上載者:User

標籤:

       有的時候填表單必須非空,那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驗證非空的一種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.