如何验证用户在CheckBoxList中至少选择了一个复选框? [英] How to validate a user chose at least one checkbox in a CheckBoxList?

查看:82
本文介绍了如何验证用户在CheckBoxList中至少选择了一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CheckBoxList控件,我想要求用户至少选中一个复选框,而不必检查每一个,三个或什至一个复选框。

I've got a CheckBoxList 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.

本着asp.net验证控件的精神,我可以用什么来强制执行此操作?我也在使用Ajax验证扩展程序,因此,如果它看起来像其他控件,而不是代码背后的一些俗气的服务器验证方法,那将是很好的选择。

In the spirit of asp.net's validation controls, what 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 只要您具有所有复选框控件共同用作选择器(例如类)(。NET控件上的CssClass),就可以轻松地完成此操作。您可以创建一个简单的 JQuery 函数,并将其连接到ASP.NET自定义验证器。请记住,如果您确实执行了自定义验证程序路线,以确保在javascript无法正常工作的情况下也对服务器端进行了检查,则不会像其他.NET验证程序一样获得免费的服务器端检查。

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.

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

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

您无需使用 JQuery ,它只会使javascript函数进行迭代并更轻松地查看所有复选框控件,但您可以根据需要使用普通javascript。

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

侧面说明: JQuery 只是一个小js文件,您需要将其添加到页面中。包含它后,您就可以使用所有喜欢的 JQuery 。无需安装,我认为下一版本的Visual Studio将完全支持它。

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.

这篇关于如何验证用户在CheckBoxList中至少选择了一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆