1

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 code works fine.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 because of the code length.

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;

I am using html button to call this

<asp:Button ID="Button2"  OnClientClick="return validated();" runat="server" Text="Enable" />

3 Answers 3

2

You could create a method validate(cal1, cal2) an call this for each date pair you wan tto validate within the validate() method

Sign up to request clarification or add additional context in comments.

Comments

2

If you pass the element IDs into the function then you could reuse it for each pair of dates.

Comments

2
function validated(){


 var EndDate1=document.getElementById('<%=EndDate1.ClientId%>').value;
 var StartDate1=document.getElementById('<%=StartDate1.ClientId%>').value;
  var EndDate2=document.getElementById('<%=EndDate2.ClientId%>').value;
 var StartDate2=document.getElementById('<%=StartDate2.ClientId%>').value;
//So on

   validatedCommon(EndDate1,StartDate1);
   validatedCommon(EndDate2,StartDate2);

//So on
}

function validatedCommon(cal1,cal2) {

         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;

1 Comment

..the code is working but the problem is when i call validatedCommon(EndDate1,StartDate1) on a button click the validatedCommon(EndDate2,StartDate2) is also begin called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.