Validate three dropdown lists control conditionally using JavaScript and ASP.Net Custom Validator

Last Reply on May 20, 2012 12:42 AM By Mudassar

Posted on May 16, 2012 11:11 PM

Hi I want to validate three dropdown lists the value of dropdown list fetched from sql database . The  folowing code i am using , it does not give any error  but not working ,please help

aspx code: <head> 
<script type="text/javascript" >
 function CustomValidateBatch(sender, args) {
var yr1 = document.getElementById("ddlBYear1");
var yr2 = document.getElementById("ddlBYear2");
var brnch = document.getElementById("ddlbranch");
branch1=brnch[e.selectedIndex].value; if ( branch1 = "MCA") {
( (document.getElementById('ddlBYear2').selectedIndex) - (document.getElementById('ddlBYear1').selectedIndex) == 3); {
} args.IsValid = true; } else args.IsValid = false;
} </script>
<form id="form1" runat="server">
<div> <tr> <td> Branch : </td>
<td> <asp:DropDownList ID="ddlbranch" runat="server" OnSelectedIndexChanged="ddlbranch_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList> </td> <td>
<asp:CustomValidator ID="CustomValidator5" runat="server" ErrorMessage="please select branch" ClientValidationFunction="ValidateBranch" ValidationGroup="one">
</asp:CustomValidator> </td> </tr>
<tr> <td> Batch : </td>
<td> <asp:DropDownList ID="ddlBYear1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBYear1_SelectedIndexChanged"> </asp:DropDownList> </td>
<td> <asp:DropDownList ID="ddlBYear2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBYear2_SelectedIndexChanged"> </asp:DropDownList>
 <td> </td> </td> </tr> <tr> <td> </td> <td>
 <asp:CustomValidator ID="CustomValidator6" runat="server" ErrorMessage="please select start year !" ClientValidationFunction="ValidateBYear1" ValidationGroup="one"></asp:CustomValidator> </td>
 <td> <asp:CustomValidator ID="CustomValidator7" runat="server" ErrorMessage="please select end year" ClientValidationFunction="ValidateBYear2" ValidationGroup="one"></asp:CustomValidator> </td> </tr> <tr>
<td></td> <td> <asp:CustomValidator ID="CustomValidator8" runat="server" ErrorMessage="Start year must be less from End year" ControlToValidate="ddlBYear1" ValidationGroup="one" ClientValidationFunction="CustomValidate">
</asp:CustomValidator> </td><td>
<%-- <asp:CustomValidator ID="CustomValidator9" runat="server" ClientValidationFunction="CustomValidateBatch" ControlToValidate="ddlBYear2" ValidateEmptyText="true" ErrorMessage="Batch Not valid" ></asp:CustomValidator>--%>
</td></tr> <tr> <asp:Button ID="but_Submit" runat="server" OnClick="but_Submit_Click" Text="Continue"
ValidationGroup="one" /> </div> </form> </body>


Posted on May 20, 2012 12:42 AM
<script type="text/javascript" >
        function CustomValidateBatch(sender, args) {
            var yr1 = document.getElementById('ddlBYear1');
            var yr2 = document.getElementById('ddlBYear2');
            var brnch = document.getElementById('ddlbranch');
            if ( branch1 == "MCA")
              ( (document.getElementById('ddlBYear2').value) - (document.getElementById('ddlBYear1').value) == 3); {}
                args.IsValid = true;
                 args.IsValid = false;


I agree, here is the link: