Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What kind of validation do I have to use for dropdown list if the user did not select any option from the dropdown list?
Posted
Updated 14-Sep-10 21:49pm
v2
Comments
Dalek Dave 15-Sep-10 3:49am    
Minor Edit for Grammar.

Use RequiredFieldValidator and set the initial value property of RequiredFieldValidator( usually this will be 'Select').

Hope this will help you :)
 
Share this answer
 
You can also use Javascript to see the selectedIndex. If it's '-1'(if nothing is selected) or '0'(if option 'Select' is selected) then show alert and not allow the submit.

-1 or 0 would be based on how you have implemented the dropdown.
 
Share this answer
 
As the others have stated, or, alternatively, set a default.
 
Share this answer
 
You can do it by using both javascript and c#.
Here i am using c#.
This is a Very simple example for you..
i have taken 1 button,1label,1 dropdownlist on which i am applying requiredfieldvalidator............


Design

XML
<asp:DropDownList ID="DDlist" runat="server">
   <asp:ListItem   Text="select" Value="None"></asp:ListItem>
   <asp:ListItem Text="Microsoft" Value=" Name"></asp:ListItem>
   <asp:ListItem Text="C#" Value="book "></asp:ListItem>
   </asp:DropDownList>
   <br />
   <asp:RequiredFieldValidator ID="DDListvalidate" Text="Select A value from list" ControlToValidate ="DDlist" InitialValue="None"  runat="server">
   </asp:RequiredFieldValidator>
   <br />
   <asp:Label ID="LblShow" Text="LblShow" runat="server">

   </asp:Label>
   <asp:Button ID="Btn1"  Text="ValidateDDList" runat="server" onclick="Btn1_Click" />


Code

C#
protected void Btn1_Click(object sender, EventArgs e)
{
    LblShow.Text = DDlist.SelectedItem.Text;
}


here what i am doing is,i have assigned value none in dropdownlist and also set it as the initial value for requiredfieldvalidator.....

now whenever you click om button it check dropdownlist ,if it found select here .
it will show error msg.
otherwise show the selected value in the label............
hope it will help you .
 
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