ASP.NET - validation for dropdown - Asked By msakt on 08-Jun-12 04:21 AM

validation for dropdown when click checkbox inside gridview
Jitendra Faye replied to msakt on 08-Jun-12 04:30 AM
Try like this-

<script type="text/javascript">

function funCheck(src, args) {
var val= document.getElementById('<%=ddl1.ClientID %>');
if (val.value="") //Here u are checking any of the DDL is not selected
{
alert("select any value");
}
else

}
</script>

dipa ahuja replied to msakt on 08-Jun-12 04:39 AM
 <asp:DropDownList ID="ddlaccesstype" runat="server" ValidationGroup="Chk" Width="171px">
    <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
    <asp:ListItem Text="Admin" Value="1"></asp:ListItem>
    <asp:ListItem Text="Supervisor" Value="2"></asp:ListItem>      
  </asp:DropDownList>
<asp:RequiredFieldValidator ID="reqFavoriteColor" Text="(Required)" InitialValue="0"
    ControlToValidate="ddlaccesstype" runat="server" />
 
Using javascript
 
<script type="text/javascript">
  function Validate() {
    var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
    var option = ddl.options[ddl.selectedIndex];
    alert(option.value);
    if (option.value == 0) {
      alert('select value from dropdown');
      return false;
    }
    else {
      return true;
    }
  }
</script>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate();" />