Home
Manage Your Code
Snippet: Custom Validator for From Date <= To Date (ASP.NET)
Title: Custom Validator for From Date <= To Date Language: ASP.NET
Description: This code ensures that the From Date is less than or equal to the To date in date ranges using the ASP.NET Custom Validator Views: 299
Author: Thomas Jenkins Date Added: 9/29/2008
Copy Code  
    <script type="text/javascript">
        function ShowModalPopup(target)
        {
            $find(target).show(); 
             
             return false;
        }
        
         function CheckDatesMinLessMax(source, arg)
        {
            var fromdate = document.getElementById('<%=frmTxtEducationSchoolDegreeDateFrom.ClientID %>');
            var todate = document.getElementById('<%=frmTxtEducationSchoolDegreeDateTo.ClientID %>');
           
             if(fromdate.value <= todate.value)
		arg.IsValid = true;
	      else
		arg.IsValid = false;
        }
        
        
    </script>
    <asp:CustomValidator ID="cvEducationDates" runat="server" ClientValidationFunction="CheckDatesMinLessMax" ValidationGroup="addEducation" Display="Dynamic"><div class="validationError"><span></span>Date From needs to be before Date To.</div></asp:CustomValidator>    
Usage
Add the javascript to the top of the page and add the custom validator where you want to display the error message. I put it just below the field container div