开发者

Customvalidator: Check if radiobuttonlist contains a selected item

开发者 https://www.devze.com 2022-12-31 14:53 出处:网络
I have a radiobuttonlist with two items, Yes or No. The radiobuttonlist control has a customvalidator that needs a servervalidation function and a javascript clientvalidationfunction. Could you help m

I have a radiobuttonlist with two items, Yes or No. The radiobuttonlist control has a customvalidator that needs a servervalidation function and a javascript clientvalidationfunction. Could you help me? The function in this message works but only when i actually have choosen one of the two listitems, when no listitem is selected the validation skips my radiobuttonlist control.

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

EDIT: Relevant code from comments

<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" RepeatLayou开发者_运维技巧t="Flow"
      RepeatDirection="Horizontal" TabIndex="4">
    <asp:ListItem Text="Yes" Value="1" />
    <asp:ListItem Text="No" Value="0" />
</asp:RadioButtonList>

Code Behind:

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


Have you set the ValidateEmptyText Property of the CustomValidator to true?

edit: Have you set the CausesValidation Property of your Submit-Button/RadioButtonList to true? Please provide some code from your aspx-page.


Here is another javascript clientvalidation function i have tried:

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


Do you need to use client-side?

Here is a server-side solution...

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

CodeBehind:

protected void cmpTermsAccepted_ServerValidate(Object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
    args.IsValid = (args.Value == "Agree");
}


That should work. Trying taking the control to validate property off the customer validator.


<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" />

And the script, is much shorter because of jquery. This will do what you want.

  <script>
                        function LocationAccurate_ClientValidate(sender, e) {
                            e.IsValid = $("#<%=LocationAccurateRBL.ClientID%> > input").is(':checked');
                        }                          
                    </script>
0

精彩评论

暂无评论...
验证码 换一张
取 消