Asp.net RequiredFieldValidator validate to RadioButtonList

Asp.net RequiredFieldValidator(how to validate RadioButtonList):



In this article we learn how to validate a RadioButtonList using JavaScript in ASP.NET. As we know that JavaScript provides a way to validate a web page’s data on the client's computer before sending it to the web server.

Other Radiobuttonlist related  example in asp.net:


To validate RadioButtonList in JavaScript:

function getCheckedRadioButton() 
{
    var radio = document.getElementsByName("RadioButtonList1");                           
    for (var i = 0; i < radio.length; i++) 
       if (radio[i].checked) { 
       alert("Radio button having value " + radio[i].value + " was checked."); // Show the checked value
       return true;
       }
    }   
  alert("Not checked any radio button");    
}


Validating RadioButtonList:


<%@ 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">
<script runat="server"> 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Select one : ";
        Label1.Text += RadioButtonList1.SelectedItem.Text.ToString();
    } 
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net RequiredFieldValidator example: how to validate RadioButtonList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color: Green">
            RadioButtonList Validation in asp.net using C#</h2>
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Red"> 
        </asp:Label>
        <br />
        <br />
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="3"
            ForeColor="Black" BorderColor="DarkBlue" BorderWidth="2">
            <asp:ListItem>CheckBoxList</asp:ListItem>
            <asp:ListItem>TreeView</asp:ListItem>
            <asp:ListItem>Button</asp:ListItem>
            <asp:ListItem>SqlDataSource</asp:ListItem>
            <asp:ListItem>GridView</asp:ListItem>
            <asp:ListItem>Calendar</asp:ListItem>
            <asp:ListItem>BulletedList</asp:ListItem>
        </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="ReqiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1"
            ErrorMessage="Select your favorite!"> 
        </asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="Button1" runat="server" ForeColor="Red" Text="Submit "
            OnClick="Button1_Click" Width="165px" />
    </div>
    </form>
</body>
</html>
 
Asp.net RequiredFieldValidator to RadioButtonList
Asp.net RequiredFieldValidator to RadioButtonList

Other asp.net related Post:


Comments

Popular posts from this blog