-1

Can Some one Please help with the below?

I got below Regex expression to validate "MM/DD/YYYY" from <a href="http://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy/31613437#31613437?newreg=51e1ed3c1b2d468fa7a1cab86a40eb51">clickhere</a>, but I need the validation of year to be from 1900 and dates should be a valid calendar date.

Regular Expression

/^(?:(?:(?:0[13578]|1[02])(\/)31)\1|(?:(?:0[1,3-9]|1[0-2])(\/)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:02(\/)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/)(?:0[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/
Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114
sunny
  • 99
  • 2

2 Answers2

1

May I suggest using moment js for date validation instead of a regex, they have excellent support for what you are after:

var date1 = "25/01/1899";
var date2 = "25/01/1900";

function validateDate(date, format) {
 if (!format) format = "DD/MM/YYYY";
  var m = moment(date, format);
  return m.isValid() && m.isAfter("1900-01-01");
}

console.log(validateDate(date1));
console.log(validateDate(date2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment-with-locales.min.js"></script>
baao
  • 62,535
  • 14
  • 113
  • 168
  • Thanks for the answer. But i cannot use other java scripts as of now in my current system. – sunny Oct 03 '16 at 11:19
0

Maybe you could try checking if the date is a valid date (you can find a few tips here: Detecting an "invalid date" Date instance in JavaScript)

And then, if the date is valid, check if it's greater than 1900.

Community
  • 1
  • 1
Alan Jurczak
  • 439
  • 5
  • 14