Please enable Javascript to correctly display the contents on Dot Net Tricks!

Validate ListBox and DropDown List using Required Field Validator

  Author : Shailendra Chauhan
Updated On : 24 Sep 2016
Total Views : 40,829   
 

In Asp.net, sometimes we need to validate ListBox and DropDown List using Required field validator. It is tricky and avoids the use of JavaScript function to validate ListBox and DropDown List. Here I am sharing the code.

Validate DropDown List and ListBox

 <form id="form1" runat="server">
<div>
 <asp:ListBox ID="lstboxCity" runat="server" Width="200px" SelectionMode="Multiple">
 <asp:ListItem Value="1">Delhi</asp:ListItem>
 <asp:ListItem Value="2">Noida</asp:ListItem>
 <asp:ListItem Value="3">Gurgaon</asp:ListItem>
 <asp:ListItem Value="4">Mumbai</asp:ListItem>
 <asp:ListItem Value="5">Pune</asp:ListItem>
 <asp:ListItem Value="6">Banglore</asp:ListItem>
 <asp:ListItem Value="7">Lucknow</asp:ListItem>
 </asp:ListBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="lstboxCity" InitialValue="" runat="server" ErrorMessage="Please select atleast one city"></asp:RequiredFieldValidator>
</div>
<br />
<div>
 <asp:DropDownList ID="ddlCity" runat="server" Width="200px">
 <asp:ListItem Value="0">--Select--</asp:ListItem>
 <asp:ListItem Value="1">Delhi</asp:ListItem>
 <asp:ListItem Value="2">Noida</asp:ListItem>
 <asp:ListItem Value="3">Gurgaon</asp:ListItem>
 <asp:ListItem Value="4">Mumbai</asp:ListItem>
 <asp:ListItem Value="5">Pune</asp:ListItem>
 <asp:ListItem Value="6">Banglore</asp:ListItem>
 <asp:ListItem Value="7">Lucknow</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ddlCity" InitialValue="0" runat="server" ErrorMessage="Please select city"></asp:RequiredFieldValidator>
 </div>
 <br />
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
 </form> 

Note

For validating DropDown List, make sure you have "--Select--" option value 0 other wise validation on it will not work.

Summary

In this article I try to explain how to validate DropDownList and ListBox using Required Field Validator. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+