Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have radio button list (size-3),
if i select radiobutton1 then only textbox1 validion should be work,
if i select radiobutton2 then only textbox2 validion should be work,
if i select radiobutton3 then only textbox3 validion should be work,
Posted

You can check which radio button value is selected and based on that you could add something that sets the "ControlToValidate" property of your validator to the particular control you want.
protected void submitButton_Click(object sender, EventArgs e)
{
     if(radiobuttonlist.SeletedValue == "rbl1Value1")
     {
         validatorControl.ControlToValidate = "TextBox1";
     }
     else if (radiobuttonlist.SeletedValue == "rbl1Value2")
     {
         validatorControl.ControlToValidate = "TextBox2";
     }
     else
     {
         validatorControl.ControlToValidate = "TextBox3";
     }

}


This is very basic and others might have better ideas.
 
Share this answer
 
v3
Comments
surendra4u 15-Aug-13 19:12pm    
I used the below code from your reference code, but i got validation errors that validanitions are working at a time


/////Script//////
void cardValidation(object source, ServerValidateEventArgs args)
{
string txt;
txt=Text1.Text;
if (RadioButtonList1.SelectedValue == "1")
{
if (txt.Length == 15)
args.IsValid = true;

else
args.IsValid = false;

}
else if (RadioButtonList1.SelectedValue == "2")
{
if (txt.Length == 16)
args.IsValid = true;

else
args.IsValid = false;
}
else args.IsValid = false;
}

</script>


/////html////

<form id="form1" runat="server">

<asp:RadioButtonList ID="RadioButtonList2" runat="server"
onselectedindexchanged="RadioButtonList2_SelectedIndexChanged">
<asp:ListItem Value="1">Credit Card
<asp:ListItem Value="2">E.F.T.
<asp:ListItem Value="3">Coupon


<asp:Label ID="Label2" runat="server" Text="Credir Card">


<asp:RadioButtonList
ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1">amex
<asp:ListItem Value="2">other


<asp:TextBox id="Text1" runat="server" />

<asp:RegularExpressionValidator ID="REt1" runat="server" ControlToValidate="Text1" ValidationExpression="^\d+$" ErrorMessage="Numbers only " Font-Bold="True" ForeColor="Red" >

<asp:CustomValidator id="CardValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Enter valid Card number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
OnServerValidate="cardValidation"
runat="server"/>


<asp:Label ID="Label3" runat="server" Text="E.F.T.">

<asp:TextBox ID="TextBox1" runat="server">
<asp:RequiredFieldValidator ID="RFVtb1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Required!" Font-Bold="True" ForeColor="Red">


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ValidationExpression="^\d+$" ErrorMessage="Numbers only " Font-Bold="True" ForeColor="Red" >


<asp:Label ID="Label4" runat="server" Text="Coupon">

<asp:TextBox ID="TextBox2" runat="server">
<asp:RequiredFieldValidator ID="RFiV2" runat="server" ControlToValidate="TextBox2" ErrorMessage="Required!" Font-Bold="True" ForeColor="Red">


<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>

<asp:Label ID="Label1" runat="server" Text="Label">

</form>


/////.cs//////

protected void ValidateBtn_OnClick(object sender, EventArgs e)
{
/*
Response.Redirect("https://www.facebook.co.in");
if (Page.IsValid)
{

Response.Redirect("https://www.google.co.in");

}

else
{

//Message.Text = "Page is not valid!";

}
* */
int x = 0;

if (RadioButtonList2.SelectedValue == "1")
{
REt1.ControlToValidate = "Text1";
CardValidator1.ControlToValidate = "Text1";
if (Page.IsValid)
{

x = 1;//true;
}
}
else if (RadioButtonList2.SelectedValue == "2")
{
RFVtb1.Contr
On Client side, you can validate the textbox based on selected radio button using some javascript. And on server as well, you can apply the same logic, verify only that textbox which is required to be validated
 
Share this answer
 

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