Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The below code is used to validate start and end date(two textbox and date is selected from calendar)
when a button is clicked.The problem is i have 8 more start and end date to validate.so can anyone tell me a common code to validate all the start and end date
JavaScript
function validated() {
       var cal1=document.getElementById('<%=EndDateTwo.ClientId%>').value;
       var cal2= document.getElementById('<%=StartDateTwo.ClientId%>').value;
             
                
                if (cal1 == '' || cal2 == '') {
                    alert("Start Date and End Date  can not bleft blank.");
                    return false;
                }
                var dt1 = Date.parse(cal1);
                var dt2 = Date.parse(cal2);
                if (dt1 <= dt2) {
                    alert("End Date must occur after the Start date ");
                    return false;
                }
                return true;

ASP.NET
<asp:TextBox ID="StartDateTwo" runat="server"></asp:TextBox>
<asp:HyperLink ID="hypCalenStartTwo" runat="server" ImageUrl="~/images/ico-cal.gif"></asp:HyperLink>
<ajax:CalendarExtender ID="StartDatePickerTwo"  runat="server" PopupButtonID="hypCalenStartTwo"
TargetControlID="StartDateTwo" SelectedDate='<%# Datetime.Today() %>' Format="MM/dd/yyyy">
</ajax:CalendarExtender>

<asp:TextBox ID="EndDateTwo" runat="server" ></asp:TextBox>
<asp:HyperLink ID="HypCalenEndTwo" runat="server" ImageUrl="~/images/ico-cal.gif"></asp:HyperLink>
 <ajax:CalendarExtender ID="EndDatePickertwo"  runat="server" PopupButtonID="hypCalenEndTwo"
 TargetControlID="EndDateTwo" SelectedDate="<%# Datetime.Today() %>" Format="MM/dd/yyyy">
</ajax:CalendarExtender>
Posted
Updated 18-Jun-15 21:13pm
v5
Comments
Sreekanth Mothukuru 19-Jun-15 7:15am    
how are you calling validate method and from where ?
cafateria 19-Jun-15 7:55am    
<asp:Button ID="Button3" OnClientClick="return validated()" runat="server" Text="Enable" />.i am using this to call
Sreekanth Mothukuru 19-Jun-15 8:44am    
Use jQuery validation along with custom validation method.

http://jqueryvalidation.org/jQuery.validator.addMethod/
Sreekanth Mothukuru 19-Jun-15 7:18am    
I suggest you to generalize the validate method and call it by passing controls as parameters

function validated(cal1, cal2) {
...
}

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