Click here to Skip to main content
Sign Up to vote bad
good
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:17
Edited 4 Oct '12 - 0:42


3 solutions

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:10
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:16
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:23
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:41
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" />
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:10
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:29
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:09
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:23
thanks dear... if this code is helpfull to u then accept answer.
Just add the same name in radiobutton GroupName property
  Permalink  
Comments
amitkumar5734 - 4 Oct '12 - 7:10
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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 419
1 Mahesh Bailwal 373
2 Maciej Los 255
3 CPallini 175
4 Rohan Leuva 175
0 Sergey Alexandrovich Kryukov 9,287
1 OriginalGriff 7,204
2 CPallini 3,923
3 Rohan Leuva 3,211
4 Maciej Los 2,713


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid