开发者

Javascript Validation for Asp.Net Radiobutton list

开发者 https://www.devze.com 2023-02-11 08:56 出处:网络
I am using a asp.net radiobuttonlist. <asp:RadioButtonList ID=\"rbtnAEreq\" RepeatDirection=\"Horizontal\" runat=\"server\">

I am using a asp.net radiobuttonlist.

 <asp:RadioButtonList ID="rbtnAEreq" RepeatDirection="Horizontal" runat="server">
                    开发者_运维问答                            <asp:ListItem Text="Yes" Value="true"></asp:ListItem>
                                                <asp:ListItem Text="No" Value="false"></asp:ListItem>
                                            </asp:RadioButtonList>

on buttonclick event i want to validate the radiobuttonlist. My condition is either yes or no should be selected. How to do it in javascript...


Make it easier on yourself, and instead of writing JavaScript yourself, simply use a RequiredFieldValidator and set EnableClientScript = "true".

It's described here, among other places: http://www.geekpedia.com/tutorial82_Validate-using-RequiredFieldValidator.html

The control exists, out of the box, with the standard Validation controls, so make it easy on yourself and don't re-invent the wheel (unless you can make a better wheel).

Edit

You dind't mention whether you are able to use JQuery for the scripting. If you are, then you can do this via JQuery. http://www.shawnduggan.com/?p=126 It works the same for a RadioButtonList as a CheckBoxList.

And finally, if JQuery is also not an option and you REALLY want ot use javaScript, you can get the selected value of the drop-down list and compare it to your expected value. The code for getting the selected value is simple. You can use this code to test getting hte value, and them nodify it to compare the value in your validation routine.

function GetRadioButtonValue(id)
{
    var radio = document.getElementsByName(id);
    for (var j = 0; j < radio.length; j++)
    {
        if (radio[j].checked)
            alert(radio[j].value);
    }
}
0

精彩评论

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

关注公众号