如何验证用户选择至少一个复选框?

编程入门 行业动态 更新时间:2024-10-23 21:34:05
本文介绍了如何验证用户选择至少一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有,我想要求用户检查至少一盒复选框组控制,如果他们检查每一个人,或3,甚至是无所谓只是一个。

在asp的验证控件的精神,我可以用它来执行这一点。我还使用了Ajax验证扩展,所以这将是很好,如果它可能看起来像其他控件,以及在codebehind不是有些俗气服务器validate方法。

< ASP:的CheckBoxList RepeatDirection =水平RepeatLayout =表的RepeatColumns =3ID =ckBoxListReasons=服务器>     < ASP:列表项文本=preliminary建设值=prelim_construction/>     < ASP:列表项文本=最终建设VALUE =final_construction/>     < ASP:列表项文本=建设改造VALUE =construction_alteration/>     < ASP:列表项文本=重塑VALUE =改造/>     < ASP:列表项文本=颜色值=颜色/>     < ASP:列表项文本=砖VALUE =砖/>     < ASP:列表项文本=外部照明值=exterior_lighting/>     < ASP:列表项文本=甲板/庭院/烫平VALUE =deck_patio_flatwork/>     < ASP:列表项文本=栅栏/筛选VALUE =fence_screening/>     < ASP:列表项文本=山水 - 前VALUE =landscape_front/>     < ASP:列表项文本=山水 - 侧/后值=landscape_side_rear/>     < ASP:列表项文本=其他值=其他/> < / ASP:的CheckBoxList>

解决方案

这很容易做到这一点的验证服务器端,但我假设你想这样做客户端?

JQuery的可以做到这一点很容易,只要你有什么事情,所有的CheckBox控件的共同点作为选择使用,如类(的CssClass在你的.NET控件)。你可以做一个简单的 JQuery的功能,并将其连接到一个ASP.NET自定义验证。请记住,如果你去自定义验证途径,以确保您在JavaScript的情况下不工作检查服务器端,以及,你没有得到一个免费的服务器端检查像其他.NET验证。

有关自定义验证器的更多信息请查看以下链接: www.asp 和 MSDN

您不需要使用 JQuery的,它只是使JavaScript函数迭代,并期待在您所有的CheckBox控件更容易,但你可以只使用香草的JavaScript,如果你喜欢。

下面是一个例子,我发现在:链接到原始

< ASP:的CheckBoxList ID =chkModuleList=服务器> < / ASP:的CheckBoxList> < ASP:的CustomValidator =服务器ID =cvmodulelist   ClientValidationFunction =ValidateModuleList   的ErrorMessage =请至少选择一个模块>< / ASP:的CustomValidator> // JavaScript来添加到您的aspx页面 功能ValidateModuleList(源参数) {   VAR chkListModules =的document.getElementById('<%= chkModuleList.ClientID%>');   VAR chkListinputs = chkListModules.getElementsByTagName(输入);   对于(VAR I = 0; I< chkListinputs .length;我++)   {     如果(chkListinputs [I] .checked)     {       args.IsValid = TRUE;       返回;     }   }   args.IsValid = FALSE; }

旁注: JQuery的是一点点js文件包括您需要添加到您的网页。一旦你拥有它包含可以使用所有的 JQuery的你喜欢。没有安装,这将是完全支持在Visual Studio中,我认为下一个版本。

I've got a checkbox group control that I want to require the user to check at least ONE box, it does not matter if they check every single one, or 3, or even just one.

In the spirit of asp's validation controls can I use to enforce this. I'm also using the Ajax validation extender, so it would be nice if it could look like other controls, and not some cheesy server validate method in the codebehind.

<asp:CheckBoxList RepeatDirection="Horizontal" RepeatLayout="Table" RepeatColumns="3" ID="ckBoxListReasons" runat="server"> <asp:ListItem Text="Preliminary Construction" Value="prelim_construction" /> <asp:ListItem Text="Final Construction" Value="final_construction" /> <asp:ListItem Text="Construction Alteration" Value="construction_alteration" /> <asp:ListItem Text="Remodel" Value="remodel" /> <asp:ListItem Text="Color" Value="color" /> <asp:ListItem Text="Brick" Value="brick" /> <asp:ListItem Text="Exterior Lighting" Value="exterior_lighting" /> <asp:ListItem Text="Deck/Patio/Flatwork" Value="deck_patio_flatwork" /> <asp:ListItem Text="Fence/Screening" Value="fence_screening" /> <asp:ListItem Text="Landscape - Front" Value="landscape_front" /> <asp:ListItem Text="Landscape - Side/Rear" Value="landscape_side_rear" /> <asp:ListItem Text="Other" Value="other" /> </asp:CheckBoxList>

解决方案

It's easy to do this validation server side, but I am assuming you want to do it client side?

JQuery can do this very easily as long as you have something that all checkbox controls have in common to use as a selector such as class (CssClass on your .NET control). You can make a simple JQuery function and connect it to a ASP.NET custom validator. Remember if you do go the custom validator route to make sure you check it server side as well in case javascript is not working, you don't get a free server side check like the other .NET validators.

For more information on custom validators check out the following links: www.asp and MSDN

You don't need to use JQuery, it just makes the javascript function to iterate and look at all your checkbox controls much easier but you can just use vanilla javascript if you like.

Here is an example I found at: Link to original

<asp:CheckBoxList ID="chkModuleList"runat="server" > </asp:CheckBoxList> <asp:CustomValidator runat="server" ID="cvmodulelist" ClientValidationFunction="ValidateModuleList" ErrorMessage="Please Select Atleast one Module" ></asp:CustomValidator> // javascript to add to your aspx page function ValidateModuleList(source, args) { var chkListModules= document.getElementById ('<%= chkModuleList.ClientID %>'); var chkListinputs = chkListModules.getElementsByTagName("input"); for (var i=0;i<chkListinputs .length;i++) { if (chkListinputs [i].checked) { args.IsValid = true; return; } } args.IsValid = false; }

Side Note: JQuery is just a little js file include you need to add to your page. Once you have it included you can use all the JQuery you like. Nothing to install and it will be full supported in the next version of Visual Studio I think.

更多推荐

如何验证用户选择至少一个复选框?

本文发布于:2023-11-04 00:57:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!