regularExpressionValidator for a dropdownlist

T

Tim Mackey

hi,
i have a drop down list with the first item set to "Select..." and if the
user doesn't choose a different item, i would like to show an error message
dynamically. i know how to use the validators, its just i can't figure out
the expression for how to say "anything except 'Select...' is ok". i tried
"^(Select...)" as the expression but it didn't work.

thanks for any help
tim mackey.
 
K

Ken Cox [Microsoft MVP]

Hi Tim,

You might be able to use the RangeValidator for this if you can set
the --Select-- item's value to zero:

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select--</asp:ListItem>
<asp:ListItem Value="1">Red</asp:ListItem>
<asp:ListItem Value="2">Green</asp:ListItem>
<asp:ListItem Value="3">Blue</asp:ListItem>
</asp:DropDownList>
<asp:RangeValidator id="RangeValidator1" runat="server"
ErrorMessage="You Must Select" ControlToValidate="DropDownList1"
MaximumValue="3" MinimumValue="1"></asp:RangeValidator></P>
<P>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button></P>
</form>

Ken
Microsoft MVP [ASP.NET]
 
K

Ken Cox [Microsoft MVP]

Hi Tim,

Oops. Here's a better way. Use a RequiredFieldValidator and set its
InitialValue property to the same value as the --Select-- item.

Ken

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select--</asp:ListItem>
<asp:ListItem Value="1">Red</asp:ListItem>
<asp:ListItem Value="2">Green</asp:ListItem>
<asp:ListItem Value="3">Blue</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="You must select"
InitialValue="0"></asp:RequiredFieldValidator></P>
<P>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button></P>
</form>
 
T

Tim Mackey

Hi Ken,
That's very useful, i never even knew that property was there. i should
read the docs more :)
thanks for your help
tim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top