Validating an asp:DropDownList selection has been made

20. March 2010

A tip for validating an item on a drop down list has been selected. 

The following shows a drop down list for selecting gender.  The initial setting is to '----'.  In order to validate that the gender has been selected, use the asp:RequiredFieldValidator and set the InitialValue = '----'. 

I tried using an asp:RegularExpressionValidator first, but had problems when there was a space in the value... 

 <asp:DropDownList ID="drpGender" width="100px" runat="server">

 <asp:ListItem Selected="True" Text="----" Value="----" />

<asp:ListItem Text="Female" Value="F" />

<asp:ListItem Text="Male" Value="M" />

</asp:DropDownList>

<asp:RequiredFieldValidator ID="rfvGender" runat="server" ErrorMessage="This field is required" ControlToValidate="drpGender" InitialValue="----"></asp:RequiredFieldValidator >

 

ASP.NET

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading