Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 4-Oct-12 0:42am
v2

Just add the same name in radiobutton GroupName property
 
Share this answer
 
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
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
:)
 
Share this answer
 
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.
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
 
Share this answer
 
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_27101982, 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_27101982, 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900