Click here to Skip to main content
16,016,894 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can you tell how to implement javascript validation in asp.net controls for c# language for dropdown list.
Posted

Hi, something has to be selected. so you can add a default value and use it.

Sample Code:

XML
<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();">
            <asp:ListItem Text="Select" Selected="True" />
            <asp:ListItem Text="Apple" />
            <asp:ListItem Text="Orange" />
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Here is the sample code to validate Email field from asp.net dropdownlist box. Javascript as:

JavaScript
<script language="javascript" type="text/javascript">
function validateDropList()
{
      if(document.getElementById("<%=drop1.SelectedIndex %>").value=="")
      {
                 alert("Email id can not be blank");
                document.getElementById("<%=drop1.SelectedIndex %>").focus();
                return false;
      }
}
</script>


User Interface code as:
ASP.NET
<asp:dropdownlist id="drop1" runat="server" xmlns:asp="#unknown">
... 
</asp:dropdownlist>


Code behind as:
C#
drop1.Attributes.Add("onselectedindexchanged", "return validateDropList()")


Hope, it clarifies.
 
Share this answer
 
Comments
meeraradha 23-Feb-12 23:46pm    
please dont give an example with email validation.i need just for an simple field like text box,radio button or dropdown.

label contains a name it should validate the details entering in the textbox like that.the feild should not be empty and it should contain with limited characters and no numeric should be allowed.
Hello use the onChange attribute to write a javascript.

this code might help you:

XML
<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 = "Selected option: " + x.value;
            alert(txt);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlFruits" runat="server" onChange="javascript:displaySelected();">
            <asp:ListItem Text="Apple" />
            <asp:ListItem Text="Orange" />
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
if nothing is selected in dropdownlist it should display an error message.
 
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