Click here to Skip to main content
13,556,206 members
Rate this:
 
Please Sign up or sign in to vote.
How i want to validate a dropdownlist using javascript in asp.net???
Posted 14-Dec-11 17:52pm
Updated 9-May-17 8:47am
Comments
Gopinath1005 14-Dec-11 23:54pm
   
and also how to validate a radiobutton list??
sravi559 15-Dec-11 0:48am
   
why do u need javascript..? Cant u go for required field validator controls, which would make the task easy..!
Ofcourse u can do it using javascript like those specified below, but rfv would be easy and recommended i guess.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

Hi,

Use below JS script

function JSFunctionValidate()
{
if(document.getElementById('<%=ddlView.ClientID%>').selectedIndex == 0)
{
alert("Please select ddl");
return false;
}
return true;
}


if you want to check with value then write
objDDl = document.getElementById('<%=ddlView.ClientID%>');
if(objDDl.options[objDDl.selectedIndex].value == "")
{
alert("Please select ddl");
return false;
}
  Permalink  
Comments
Member-7697383 6-Feb-12 6:30am
   
5up!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

it's bit easy. see the following function

function validate()
{
   if(document.getElementById("ddlList").value == "")
   {
      alert("Please select value"); // prompt user
      document.getElementById("ddlList").focus(); //set focus back to control
      return false;
   }
}
  Permalink  
v2
Comments
Member-7697383 6-Feb-12 6:29am
   
5up!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Assuming your select element looks like this
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>

function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;
//if you need text to be compared then use
var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0) //for text use if(strUser1=="Select")
{
alert("Please select a user");
}
}
  Permalink  
Comments
Member-7697383 6-Feb-12 6:29am
   
5up!
Anuja-Pawar-Indore 6-Feb-12 7:23am
   
Thanks Freak
AshishChaudha 17-Dec-12 5:55am
   
my +5!
Anuja-Pawar-Indore 17-Dec-12 6:00am
   
Thanks Ashish :)
User-9036103 28-Feb-14 23:54pm
   
MY 5
AMITaspnet 26-Sep-14 3:04am
   
http://www.codeproject.com/Answers/823345/Please-Tell-Me-When-I-Click-First-Dropdownlist-val#answer1
Anuja-Pawar-Indore 26-Sep-14 4:09am
   
Are you asking or informing something?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

function validateForm()
{
var listBoxSelection=document.forms["myForm"]["select"].value;
if(listBoxSelection==0)
  {
  alert("Please select a designation");
  return false;
  }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 11

using jQuery you can do it in this way.

 function validateRadio() {
    var flag = false;
  $('#<%=RadioButtonList1.ClientID%> input').each(function(){
     if($(this).is(":checked"))
     flag = true;
   });
  return flag;
 }

function validateDropList() {
    if ($('#<%=DropDownList1.ClientID%>').val() == "") {
        return false;
    }
    else
        return true;
}
function submitForm() {
    if (!validateRadio()) {
        alert("Please do mark option.");
        return false; //do not submit form
    }
    else if(!validateDropList()){
     alert("Please do select a country.");
     return false; //do not submit form
    }
    else
        return true;


Using java script you can do it in this way.

function validateRadio() {
            var flag = false;
            var list = document.getElementById("<%=RadioButtonList1.ClientID%>"); //Client ID of the radiolist
            var inputs = list.getElementsByTagName("input");
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].checked) {
                    flag = true;
                    break;
                }
            }         
            return flag;
    }

    function validateDropList() {

        if (document.getElementById("<%=DropDownList1.ClientID%>").value == "") {              
                return false;
            }
            else
                return true;
        }
        function submitForm() {
            if (!validateRadio()) {
                alert("Please do mark option.");
                return false; //do not submit form
            }
            else if(!validateDropList()){
             alert("Please do select a country.");
             return false; //do not submit form
            }               
            else
                return true;
        }



my asp form elements are.
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
        <asp:listitem value="a">A</asp:listitem>
        <asp:listitem value="b">B</asp:listitem>
        <asp:listitem value="c">C</asp:listitem>
        <asp:listitem value="d">D</asp:listitem>
    </asp:radiobuttonlist>
        <br />
        <br />
        <div>
        <asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
            <asp:listitem value="">select</asp:listitem>
            <asp:listitem value="ind">India</asp:listitem>
            <asp:listitem value="pak">PAK</asp:listitem>
            <asp:listitem value="usa">US</asp:listitem>
            <asp:listitem value="uae">UAE</asp:listitem>
        </asp:dropdownlist>
        <br />
        <br />
            <asp:button id="submitForm" runat="server" onclientclick="submitForm()" text="Submit" xmlns:asp="#unknown" />
 </div>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

function validateListBox(){

var listBoxSelection=document.getElementById("dropDownDays").value;

if(listBoxSelection==0){

alert("Please select a day");

return false;

}else{

alert("Ok your selection is valid");

return true;

}

return true;

}
  Permalink  
Comments
Gopinath1005 15-Dec-11 0:02am
   
boss its not working..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Try this :
<script type="text/javascript" language="javascript">
   function Validation(){
        var obj;
        obj = document.getElementById("<%=ddlID.ClientID%>");
        if (!obj_Subject.value) {
          alert("Your message here");
         return false;
     }
   }



Then you can call this function where you want.
for example, you can call it from a button click like this :
btnID.Attributes.Add("onClick", "return Validation()");
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

try this:
using Javascript validation for Drop down list:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function displaySelected() {
            var x = document.getElementById('<%=ddlFruits.ClientID%>');
            var txt;
            if (x.value == 'Select') {
                txt = "Please select any fruit";
            }
            else {
                txt = "Selected option: " + x.value;
            }
            alert(txt);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:dropdownlist id="ddlFruits" runat="server" onchange="javascript:displaySelected();" xmlns:asp="#unknown">
            <asp:listitem text="Select" selected="True" />
            <asp:listitem text="Apple" />
            <asp:listitem text="Orange" />
        </asp:dropdownlist>
    </div>
    </form>
</body>
</html>


you can also validate using compare validator like below :
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">          
        <asp:listitem value="-1">Select</asp:listitem>
        <asp:listitem>Yasser</asp:listitem>
        <asp:listitem>Zaid</asp:listitem>
    </asp:dropdownlist>
    <asp:comparevalidator id="CompareValidator1" runat="server" controltovalidate="DropDownList1" valuetocompare="-1" xmlns:asp="#unknown">
    ErrorMessage="Field is required" Operator="NotEqual" ></asp:comparevalidator>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

 On page source code
<script language="javascript" type="text/javascript">
        function validate() {

            var summary = "";
            
            summary += validatelocation();
            
            if (summary != "") {
                alert(summary);
                return false;
            }
            else {
                return true;
            }

        }
function validatelocation() {
            var uid;
            var temp = document.getElementById("<%=ddllocation.ClientID %>");
            uid = temp.value;
            if (uid == "") {
                return ("Please enter location" + "\n");
            }
            else {
                return "";
            }
        }



On aspx.cs page

 protected void Page_Load(object sender, EventArgs e)
        {
          ImageButton1.Attributes.Add("onclick", "javascript:return validate()");
        }
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 14

Try this

$('#PageContent_ddllist').focus(function () {
if (this.value == '--Select--') {
$('.fld1-error').html('This field is required');
document.getElementById('PageContent_ddllist').focus();
$('#PageContent_ddllist').css('border-color', 'red');
return false;
} else {
$('.fld1-error').html('');
$('#PageContent_ddllist').css('border-color', 'green');

}
});
  Permalink  
Comments
DeepuSNair 8-Jan-15 0:40am
   
what is the need of answering old questions?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.180515.1 | Last Updated 9 May 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100