Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
How i want to validate a dropdownlist using javascript in asp.net???
Posted
Updated 9-May-17 8:47am
Comments
Gopinath1005 14-Dec-11 23:54pm    
and also how to validate a radiobutton list??
Sravan S 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.

Hi,

Use below JS script

JavaScript
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
JavaScript
objDDl = document.getElementById('<%=ddlView.ClientID%>');
if(objDDl.options[objDDl.selectedIndex].value == "")
{
alert("Please select ddl");
return false;
}
 
Share this answer
 
Comments
Tech Code Freak 6-Feb-12 6:30am    
5up!
it's bit easy. see the following function

JavaScript
function validate()
{
   if(document.getElementById("ddlList").value == "")
   {
      alert("Please select value"); // prompt user
      document.getElementById("ddlList").focus(); //set focus back to control
      return false;
   }
}
 
Share this answer
 
v2
Comments
Tech Code Freak 6-Feb-12 6:29am    
5up!
Assuming your select element looks like this
XML
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>

JavaScript
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");
}
}
 
Share this answer
 
Comments
Tech Code Freak 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 :)
Member 9080845 28-Feb-14 23:54pm    
MY 5
C#
function validateForm()
{
var listBoxSelection=document.forms["myForm"]["select"].value;
if(listBoxSelection==0)
  {
  alert("Please select a designation");
  return false;
  }
}
 
Share this answer
 
using jQuery you can do it in this way.

JavaScript
 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.

JavaScript
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.NET
<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>
 
Share this answer
 
C#
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;

}
 
Share this answer
 
Comments
Gopinath1005 15-Dec-11 0:02am    
boss its not working..
Try this :
JavaScript
<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 :
C#
btnID.Attributes.Add("onClick", "return Validation()");
 
Share this answer
 
try this:
using Javascript validation for Drop down list:
JavaScript
<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.NET
<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>
 
Share this answer
 
C#
 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()");
        }
 
Share this answer
 
v2
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');

}
});
 
Share this answer
 
Comments
Deepu S Nair 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900