views:

472

answers:

3

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" RepeatLayout="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
+1  A: 

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.

Tim Schmelter
No but now the ValidateEmptyText property is set to true, and it still not validates when clicking the submit button. The radiobuttonlist is only validated by my javascript function when clicking one of the radiobuttonlist items...
EasyDot
edited my post...
Tim Schmelter
<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>
EasyDot
<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>
EasyDot
The javascript clientvalidation function is located in a external .js file<script language="javascript" type="text/javascript" src="Scripts/Javascripts.js"></script>
EasyDot
I have a server validation function that dosent work to... Public Sub RadioButtonListServerValidation(ByVal sender As Object, ByVal args As ServerValidateEventArgs) If rblBusinessCritical.SelectedValue = "-1" Then args.IsValid = False cvRadioButtonList.ErrorMessage = "Business critical needed (servervalidation)" Exit Sub Else args.IsValid = True End If End Sub
EasyDot
Now the clientvalidation function works, stupid as i am i only set ValidateEmptyText property to true on the submit button and not on the customvalidator... Thanks :)
EasyDot
Ok, and dont forget to set the EnableClientScript property from the validator to true either and this question as answered if it is ;-)
Tim Schmelter
And for the future: add additional source code to your original question, so that others dont have to search for it in the comments.
Tim Schmelter
A: 

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;
}
EasyDot
A: 

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");
}
Konrad