0

我有一个带有两个项目的单选按钮列表,是或否。单选按钮列表控件有一个自定义验证器,它需要一个服务器验证功能和一个 javascript 客户端验证功能。你可以帮帮我吗?此消息中的功能有效,但仅当我实际选择了两个列表项之一时,当未选择任何列表项时,验证会跳过我的单选按钮列表控件。

function ValidateRadioButtonList(source, arguments) {
        var RBL = document.getElementById(source.controltovalidate);
        var radiobuttonlist = RBL.getElementsByTagName("input");
        var counter = 0;
        var atLeast = 1
        for (var i = 0; i < radiobuttonlist.length; i++) {
            if (radiobuttonlist[i].checked) {
                counter++;
            }
        }
        if (atLeast = counter) {
            arguments.IsValid = true;
            return arguments.IsValid;
        }
        arguments.IsValid = false;
        return arguments.IsValid;
    }

编辑:评论中的相关代码

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnNormal"
      CausesValidation="True" />
<asp:CustomValidator runat="server"
      ClientValidationFunction="ValidateRadioButtonList"
      OnServerValidate="RadioButtonListServerValidation" ID="cvRadioButtonList"
      Font-Bold="True" Font-Size="Medium" ErrorMessage="Business critical"
      ControlToValidate="rblBusinessCritical">*</asp:CustomValidator>
<asp:RadioButtonList ID="rblBusinessCritical" runat="server" RepeatLayout="Flow"
      RepeatDirection="Horizontal" TabIndex="4">
    <asp:ListItem Text="Yes" Value="1" />
    <asp:ListItem Text="No" Value="0" />
</asp:RadioButtonList>

代码背后:

Public Sub RadioButtonListServerValidation(ByVal sender As Object, _ 
            ByVal args As ServerValidateEventArgs)
    If rblBusinessCritical.SelectedValue = "-1" Then
        args.IsValid = False
        cvRadioButtonList.ErrorMessage = "Business critical needed"
        Exit Sub
    Else
        args.IsValid = True
    End If
End Sub
4

5 回答 5

2

您是否将CustomValidator的ValidateEmptyText 属性设置为 true?

编辑:您是否将 Submit-Button/RadioButtonList 的 CausesValidation 属性设置为 true?请从您的 aspx 页面提供一些代码。

于 2010-05-17T20:14:56.340 回答
0

那应该行得通。尝试控制以验证客户验证器的属性。

于 2011-12-19T12:40:08.193 回答
0

这是我尝试过的另一个 javascript 客户端验证功能:

function ValidateRadioButtonList(source, arguments) {
    var RBL = document.getElementById(source.controltovalidate);
    var radio = RBL.getElementsByTagName("input");
    var isChecked = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            isChecked = true;
            break;
        }
    }
    if (!isChecked) {
        alert("Please select an item");
        arguments.IsValid = false;
    }
    arguments.IsValid = true;
}
于 2010-05-17T20:25:38.897 回答
0

你需要使用客户端吗?

这是一个服务器端解决方案......

<asp:RadioButtonList id="radTerms" runat="server">
  <asp:listitem id="optDisagree" runat="server"  value="Disagree" selected="true">I don't agree</asp:ListItem>
  <asp:listitem id="optAgree" runat="server" value="Agree">I agree</asp:ListItem>
</asp:RadioButtonList>

<asp:CustomValidator Display="Dynamic" ErrorMessage="You have to agree to the terms and conditions" ID="cmpTerms" ControlToValidate="radTerms" SetFocusOnError="true" runat="server" OnServerValidate="cmpTermsAccepted_ServerValidate">*</asp:CustomValidator>

代码隐藏:

protected void cmpTermsAccepted_ServerValidate(Object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
    args.IsValid = (args.Value == "Agree");
}
于 2010-05-18T18:00:31.323 回答
0
<asp:RadioButtonList ID="LocationAccurateRBL" CssClass="radioButtonList" RepeatDirection="Horizontal" RepeatColumns="4" RepeatLayout="Flow" runat="server">
                            <asp:ListItem  Text="Yes" Value="1" />
                            <asp:ListItem Text="No" Value="0" />
                        </asp:RadioButtonList>

 <asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true" ControlToValidate="LocationAccurateRBL"
                            ClientValidationFunction="LocationAccurate_ClientValidate" ValidateEmptyText="true"
                            Text="*" ForeColor="Red" ErrorMessage="Please let us know if the location is accurate" SetFocusOnError="true" ValidationGroup="CreateVG" />

由于 jquery,脚本要短得多。这将做你想要的。

  <script>
                        function LocationAccurate_ClientValidate(sender, e) {
                            e.IsValid = $("#<%=LocationAccurateRBL.ClientID%> > input").is(':checked');
                        }                          
                    </script>
于 2013-10-01T08:29:39.727 回答