Validating a date in a grid This function checks if the EndDate of my grid is valid or not. If it is not valid, it turns the bgcolor of the cell to red color with cell-error class.

I need some help in optimizing this block of code. It looks dirty right now.

function onAccrualEndDateChange(current) {
var endDate = $(current).val();
if (endDate != "") {
    var actualEndDate = endDate;
    if (actualEndDate != null) {
        var tempEndDate = parseInt(actualEndDate.getMonth() + 1) + "/" + actualEndDate.getDate() + "/" + actualEndDate.getFullYear();
        if (ValidateDate(tempEndDate)) {
            var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();
            if (startDate != "") {
                var actualStartDate = startDate;
                if (actualStartDate != null) {
                    var tempStartDate = parseInt(actualStartDate.getMonth() + 1) + "/" + actualStartDate.getDate() + "/" + actualStartDate.getFullYear();
                    if (ValidateDate(tempStartDate)) {
                        if (actualStartDate.getDate() &gt; actualEndDate.getDate() || actualStartDate.getMonth() &gt; actualEndDate.getMonth() || actualStartDate.getFullYear() &gt; actualEndDate.getFullYear()) {
                            $(current).closest("td").addClass("cell-error");
                        } else {
                            $(current).closest("td").removeClass("cell-error");
                        }
                    }
                    else {
                        $(current).closest("td").addClass("cell-error");
                    }
                }
                else {
                    $(current).closest("td").addClass("cell-error");
                }
            } else {
                $(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
            }
        }
        else {
            $(current).closest("td").addClass("cell-error");
        }
    }
    else {
        $(current).closest("td").addClass("cell-error");
    }
} else {
    $(current).closest("td").removeClass("cell-error");
}
}


This is the code to validate the date:

function ValidateDate(date) {
//This validates date with leap year in mm/dd/yyyy or mm/d/yyyy or m/dd/yyyy or m/d/yyyy date format.
var regex = new RegExp("^(((0?[1-9]|1[012])/(0?[1-9]|1\\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\\d)\\d{2}|0?2/29/((19|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");

return (date.match(regex));
}
This isn't "optimized", I just tried to make it more readable.
As I cant test it (really wish people would make jsbin examples to test with, buggered if Im going to do it) I have no idea if it works ;)  

function onAccrualEndDateChange(current) {
  var endDate = $(current).val();

  if (endDate === "") {
    $(current).closest("td").removeClass("cell-error");
    return;
  }
  if (endDate === null) {
    $(current).closest("td").addClass("cell-error");
    return;
  }

  var actualEndDate = {
    month: endDate.getMonth(),
    date: endDate.getDate(),
    fullYear: endDate.getFullYear()
  };

  var tempEndDate = parseInt(actualEndDate.month + 1, 10) + "/" + actualEndDate.date + "/" + actualEndDate.fullYear;

  if (!ValidateDate(tempEndDate)) {
    $(current).closest("td").addClass("cell-error");
    return;
  }

  var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();

  if (startDate === "") {
    $(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
    return;
  }
  if (startDate === null) {
    $(current).closest("td").addClass("cell-error");
    return;
  }

  var actualStartDate = {
    month: startDate.getMonth(),
    date: startDate.getDate(),
    fullYear: startDate.getFullYear()
  };

  var tempStartDate = parseInt(actualStartDate.month + 1, 10) + "/" + actualStartDate.date + "/" + actualStartDate.fullYear;

  if (!ValidateDate(tempStartDate)) {
    $(current).closest("td").addClass("cell-error");
    return;
  }

  if (actualStartDate.date &gt; actualEndDate.date || actualStartDate.month &gt; actualEndDate.month || actualStartDate.fullYear &gt; actualEndDate.fullYear) {
    $(current).closest("td").addClass("cell-error");
  } else {
    $(current).closest("td").removeClass("cell-error");
  }
}