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

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

问题描述

我有一个复选框组控件,我想要求用户至少检查一个框,如果他们检查每一个,或3,甚至只有一个没有关系。

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.

在asp.net的验证控件的精神,我可以使用来强制这。我也使用Ajax验证扩展器,所以如果它可能看起来像其他控件,而不是一些cheesy服务器验证方法在代码bebeind很好。

In the spirit of asp.net'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 可以很容易地做到这一点,只要你有一些东西,所有的复选框控件有共同使用作为选择器,如类(.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.

示例:链接到原始

<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注意: 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.

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

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