Your problem is that when it's the same month two years apart, you need to also check the day of the month.
function isValidDate (from, to) {
var months = to.getMonth() - from.getMonth() +
(12 * (to.getFullYear() - from.getFullYear()));
return months < 24 || months === 24 && to.getDate() < from.getDate();
}