I have a scenario, where user has to select any one role from radiobuttonlist to register himself as below
- Administrator
- Shop floor User
- Key User
- Others
If he selects Role "Others" from radio-button list, a text box will be appeared to enter the role manually. Can I have a RequiredFieldValidator for text-box if only user selects role "Others" else if he selects other roles validation is not required.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Required_Field_Validator_based._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<table
align="center">
<tr>
<td
colspan="2">
<h3>
Registration Form</h3>
</td>
</tr>
<tr>
<td>
Name:</td>
<td>
<asp:TextBox
ID="txtName"
runat="server"></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator2"
runat="server"
ErrorMessage="Enter
Name" ControlToValidate="txtName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Email</td>
<td>
<asp:TextBox
ID="txtEmail"
runat="server"></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator3"
runat="server"
ErrorMessage="Enter
Email" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Invaid
Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
Role</td>
<td>
<asp:RadioButtonList
ID="rbtRole"
runat="server"
RepeatDirection="Vertical"
AutoPostBack="True"
onselectedindexchanged="rbtRole_SelectedIndexChanged">
<asp:ListItem>Administrator</asp:ListItem>
<asp:ListItem>Shop
floor User</asp:ListItem>
<asp:ListItem>Key
User</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
</asp:RadioButtonList>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator4"
runat="server"
ErrorMessage="Select
role" ControlToValidate="rbtRole"></asp:RequiredFieldValidator>
</td>
</tr>
<asp:Panel
ID="panell"
runat="server"
Visible="false">
<tr>
<td></td><td><asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="Enter
role" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</td>

Dorababu MekaPosted Feb 16, 2013, 12:57 AM
Hi Satyapriya Nayak I think CausesValidation =false will not stop validation firing as you make your panel visible to false may be you didn't noticed that. Try by making the panel visible to true and hit enter in the text box with out entering any text in the textbox you can see validation fires on both. Instead of that you have to make your validation enabled to true or false RequiredFieldValidator1.Enabled = true; on others and RequiredFieldValidator1.Enabled = false; for the remaining