Validate ASP.Net RadioButtonList using JavaScript Example

Validate ASP.Net RadioButtonList using JavaScript in Asp.Net using C#:



In this post we consider an Example JavaScript  for Radiobuttonlist and validate the Radiobuttonlist  using JavaScript. As we know that javascript is a scripting language. In this asp.net tutorial Example blog we give many post related to the Radiobuttonlist control these are:

Other Radiobuttonlist related post or example:


Using JavaScript with ASP.Net RadioButtonList Control:

function ValidateRadioButtonList() {
            var isChecked = false;
            var rbl= document.getElementById("<%=rbl.ClientID%>");
            var radioButtons = rbl.getElementsByTagName("input");
            for (var i = 0; i < radioButtons.length; i++) {
                if (radioButtons[i].checked) {
                    isChecked = true;
                    break;
                }
            }

            if (!isChecked) {
                alert("Please select one from the list.");
            }
            else
            {
                return isChecked;
                alert("selected...");
            }
            return isChecked;
           
        }

Use javascript function on button onclient click event:

<asp:Button ID="btnSave" OnClientClick="return ValidateRadioButtonList();"

                        runat="server" Text="ok" Width="92px" />

RadioButtonList validate using jQuery in Asp.net:

Here we take give jquery function of performing the validation on Radiobuttonlist in asp.net page.
$(document).ready(function () {
            $('#btnSubmit').click(function () {
                if ($('#rdbtn :radio:checked').length > 0) {
                    return true;
                }
                else {
                    alert('Please chooes one ')
                    return false;
                }
            })
        })

Checkboxlist  jQuery Validate Example :

 $(document).ready(function () {
            $('#btnSubmit').click(function () {
                if ($('#chktech input:checked').length > 0) {
                    return true;
                }
                else {
                    alert('Please select atleast one')
                    return false;
                }
            })
        });
For full code and get example checkboxlist validation click here…….. 

Validate radio button list in javascript in asp.net:

JavaScript Validate Example in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Radiobuttonlist_validation.aspx.cs"
    Inherits="Radiobuttonlist_validation" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net tutorial by parijat mishra | RadioButtonList Validation using JavaScript</title>
    <script type="text/javascript">
        function ValidateRadioButtonList() {
            var isChecked = false;
            var rbl= document.getElementById("<%=rbl.ClientID%>");
            var radioButtons = rbl.getElementsByTagName("input");
            for (var i = 0; i < radioButtons.length; i++) {
                if (radioButtons[i].checked) {
                    isChecked = true;
                    break;
                }
            }

            if (!isChecked) {
                alert("Please select one from the list.");
            }
            else
            {
                return isChecked;
                alert("selected...");
            }
            return isChecked;
           
        }
    </script>
    <style type="text/css">
        .style1
        {
            width: 269px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    select minmum one
                </td>
                <td>
                    :
                </td>
                <td class="style1">
                    <asp:RadioButtonList ID="rbl" runat="server"                  RepeatDirection="Horizontal">
                        <asp:ListItem Value="1" Text="option1" />
                        <asp:ListItem Value="2" Text="option2" />
                        <asp:ListItem Value="3" Text="option3" />
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    &nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;&nbsp;
                </td>
                <td class="style1">
                    <asp:Button ID="btnSave" OnClientClick="return ValidateRadioButtonList();" runat="server"
                        Text="Save" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 
 
Validate ASP.Net RadioButtonList using JavaScript
 RadioButtonList using JavaScript

Other asp.net related Examples:




Comments