0

In my ASPX page, I've an input radiobutton control :

<input type="radio" name="rbBool" value="Yes" runat="server" id="rbpYes" />
                   Yes
                    <input type="radio" name="rbBool" value="No" runat="server" 
id="rbpNo" />
                    No

I want to do client side validation to check that the user has selected a single option when i click this button:

    <asp:Button runat="server" ID="bnSavePopup" Text="Save" 
 OnClick="BnSaveCarClassDetailsClick" ValidationGroup="CheckControls"/>

Can someone suggest how this can be done with javascript?

DeadlyDan
  • 669
  • 2
  • 8
  • 20

1 Answers1

1

you need to use the attribute OnClientClick, this will call a javascript function and will fire the serverside click event based on the return value (true/false) of the clientside function.

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validate_radios() {
            if (document.getElementById("rbpYes").value == true || document.getElementById("rbpNo").value == true) {

                return true;
            }
            else {
                alert("Please select Yes or No");
                return false;
            }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="radio" name="rbBool" value="Yes" runat="server" id="rbpYes" />
            Yes
                    <input type="radio" name="rbBool" value="No" runat="server"
                        id="rbpNo" />
            No
            <asp:Button runat="server" ID="bnSavePopup" Text="Save"
                OnClick="BnSaveCarClassDetailsClick" ValidationGroup="CheckControls" OnClientClick="validate_radios()"/>
        </div>
    </form>
</body>
Banana
  • 7,424
  • 3
  • 22
  • 43
  • 1
    You can also keep button disabled. +1 – Alex Char Sep 18 '14 at 18:14
  • 1
    This doesn't work . document.getElementById("rbpAP").value is only returning the value property. I need to know if the radio button was checked or not. – DeadlyDan Sep 19 '14 at 07:49
  • @deadlydee what do you mean, if the radio was checked the value will be true, otherwise it will be false... and it works fine for me. – Banana Sep 19 '14 at 08:01