Skip Navigation Links



Validate ASP.NET DropDownList using jQuery

Author : Virendra Dugar      Blog :jQuery By Example      Date: 7/9/2012 3:30:00 AM


In this small post, today I will show you how to validate ASP.NET DropDown List using jQuery.

Let's declare a DropDownList.
<asp:DropDownList ID="ddlList" runat="server">
   <asp:ListItem Text="Select" Value="0" />
   <asp:ListItem Text="jQuery" Value="1"></asp:ListItem>
   <asp:ListItem Text="JavaScript" Value="2"></asp:ListItem>
   <asp:ListItem Text="Prototype" Value="3"></asp:ListItem>
   <asp:ListItem Text="Dojo" Value="4"></asp:ListItem>
   <asp:ListItem Text="Mootools" Value="5"></asp:ListItem>
</asp:DropDownList>
To validate the dropdown list, all you need to do is to get the selected value and check if it is not 0. If 0, then nothing is selected so alert the user, otherwise proceed.
$(document).ready(function() {
    $('#btnValidateByVal').on('click', function(e) {
        var iVal = $('#ddlList').val();
        if (iVal == 0) 
        {
            alert('Please select any technology.');
            e.preventDefault();
        }
        else 
            alert('Well Done!!!!');
   })
})​
In the above jQuery code, the validation is done using value of the selected item. But you can also validate the dropdown using the selected item text.
$(document).ready(function() {
    $('#btnValidateByText').on('click', function(e) {
        var sText = $('#ddlList1 option:selected').text().toLowerCase();
        if (sText == 'select') 
        {
            alert('Please select any technology.');
            e.preventDefault();
        }
        else 
           alert('Well Done!!!!');
    })
})​
Note: Above both jQuery codes will work only with jQuery 1.7 or higher version as I have used .on() method to bind the click event and .on was introduced in jQuery 1.7. So if you are using below version then you can either .live() or .bind() method.

Suggested Post,
Feel free to contact me for any help related to jQuery, I will gladly help you.

Bloggers

active bloggers in the last 24 hrs. Number shown in the bracket represents number of posts published in past 24 hrs,


other authors(35)

bobbysing(6)

Firoze Shakir Photographerno1(5)

Piyal Kundu(4)

Kalyan P(3)

Shobha(3)

Abrachan Pudussery(2)

Admin(2)

Cynthia Z(2)

Fidarose Isha(2)

Meena(2)

mervin anto(2)

Neha Kumari(2)

R.D. Bhalekar(2)

Sanjeev(2)

Santosh bs(2)

SM(2)

wanderingsoul(2)

....Blogger(1)

aamjunta(1)

Aarthi(1)

Adwait Sharma(1)

Aishwarya(1)

Amarendra(1)

Ana(1)

Anand Sharma(1)

Anil(1)

Anirban(1)

Anita Menon(1)

Anu Lal(1)

Asim Kumar(1)

Aurindam Mukherjee(1)

brocas area(1)

BUTTERFLIES OF TIME(1)

Cardamom(1)

clarityy(1)

Deeps(1)

Dialect Of Heart(1)

Disha Mishra(1)

hAnGp(1)

Hari(1)

Harimohan(1)

Harish.M(1)

Heartcrossings(1)

Indrani(1)

IS(1)

jaideep khanduja(1)

Janani(1)

Keyur Seta(1)

Kratik(1)

krist0ph3r(1)

Krithi Karthi(1)

Kunal(1)

magiceye(1)

magiceye(1)

Mahendra(1)

Management Punditz(1)

Manas Khatri(1)

Manas Khatri Mastana(1)

Mitra - Meet Integrate Transform Achieve(1)

MUNZ TDT(1)

Nik P(1)

P.S. Suresh Kumar(1)

Pavan Devarakonda(1)

Pooja Sharma Rao(1)

Poonam Sharma(1)

Prabal(1)

Prahallad Panda(1)

Prahallad Panda(1)

Pratik Mandrekar(1)

Pratik Poddar(1)

Preety(1)

Priya Srinivasan(1)

Rachana(1)

Rajagopalan(1)

Rams(1)

Reema Sahay(1)

Reema Sahay(1)

Richa Singh(1)

Richard Lembard(1)

Rishabh(1)

Ritcha(1)

Robinsh Kumar(1)

S(1)

Sakshi Garg(1)

sangeeta(1)

Santhosh P(1)

sasivarali(1)

Seetha(1)

shaista dhanda(1)

Shashank(1)

Shilpa(1)

shohdah(1)

Shrutee Choudhary.(1)

Shveta(1)

Soupi(1)

Spiritual Sherpa(1)

Srinivasan Sampathkumar(1)

Sriram Khé(1)

Srivalli(1)

Sunil Jose(1)

Tarun Goel(1)

Vandana(1)

Varun Nayak(1)

Venai(1)

Vikram Karve(1)

Vivek Juneja(1)

VJ Sharma(1)

Vrushali(1)

wildflower(1)

Yash(1)


garland of Languages of India
an amalgamation of the diversified traditions
gracefully presented with novelty
http://www.haaram.com