Click here to Skip to main content
12,291,230 members (56,918 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
i have a problem with radiobutton validation . i want to use such a validation that occur when i click on submit button without selecting any one of radiobutton. like there will be two radio button that contains the text like(male, female).
and when i select male the validation would be hide and when i select none of these two then validation will be visible(validation error should be in label).
Posted 3-Oct-12 21:17pm
Edited 4-Oct-12 0:42am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Just add the same name in radiobutton GroupName property
  Permalink  
Comments
amitkumar5734 4-Oct-12 7:10am
   
not working i have mentioned that i have radio button not list as well.
there is two radio button that i want to validate . i just simply want there will be a error show when i don't select any one of these (male either famale), and when i select any one of them and click on submit button no validation will be occurred.
please do me a favour
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Wright this codeon button click event

if(Radiobutton1.checked !=true && Radiobutton2.checked !=true)
{
label1.text="Plz select Gender";
}

i think this code realy help u

Regard
Sham
Smile | :)
  Permalink  
Comments
amitkumar5734 4-Oct-12 7:10am
   
not working i have mentioned that i have radio button not list as well.
there is two radio button that i want to validate . i just simply want there will be a error show when i don't select any one of these (male either famale), and when i select any one of them and click on submit button no validation will be occurred.
please do me a favour
shambhoo kumar 4-Oct-12 7:29am
   
first of all u set both radiaobutton autopostback property true the write this code..


protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked != true && RadioButton2.Checked != true)
{
Label1.Text = "Error";
}
else
{
Label1.Text = "";
}
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
RadioButton2.Checked = false;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
RadioButton1.Checked = false;
}
}

Regard
Sham
:)
amitkumar5734 4-Oct-12 8:09am
   
THANKS A LOT MAN.. IT WAS NICE STUFF .. AND REALLY GREAT WORK.. I AM REALLY HAAPY WITH YOUR CODING THANK YOU SO MUCH...!
NOW IT'S WORKING.
shambhoo kumar 4-Oct-12 8:23am
   
thanks dear...
if this code is helpfull to u then accept answer.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

1. Add GroupName attribute and put same value for both the radio buttons.
for example: GroupName="Gender"
2. Put required field validator.

You can do the same using RadioButtonList as follows:

<asp:radiobuttonlist runat="server" id="radio" xmlns:asp="#unknown">
<%--Creaint Items to be display in Radio Button List--%>
<asp:listitem value="0">Male
<asp:listitem value="1">Female

<%--Creating RequiredFieldValidator control to validate RadioButtonList that we have created--%>


<asp:requiredfieldvalidator runat="server" id="radRfv" controltovalidate="radio" initialvalue="0" errormessage="Select one option" xmlns:asp="#unknown">

Hope this will help you

~Amol
  Permalink  
Comments
amitkumar5734 4-Oct-12 7:10am
   
not working i have mentioned that i have radio button not list as well.
there is two radio button that i want to validate . i just simply want there will be a error show when i don't select any one of these (male either famale), and when i select any one of them and click on submit button no validation will be occurred.
please do me a favour
Amol Borkar, Pune, India 4-Oct-12 7:16am
   
Following approach can make your life easier
1. Put GroupName="Gender" for both the radio buttons.
2. Keep Male as by default selected.
This way one radio is selected at a time always
amitkumar5734 4-Oct-12 7:23am
   
yeh thanks ...it can work nicely.. but really man i don't want that function in my form..i just want to give an option to user to check your gender..
it can't be solution of my problem...
thanks.
Amol Borkar, Pune, India 4-Oct-12 7:41am
   
Copy following code as it is and check:

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Male" Value="0">
<asp:ListItem Text="Female" Value="1">

<asp:requiredfieldvalidator runat="server" id="radRfv" controltovalidate="RadioButtonList1" errormessage="Select one option">
<asp:Button ID="Button1" runat="server" Text="Button" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160518.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100