Skip to content

Instantly share code, notes, and snippets.

@mir4ef
Last active July 15, 2016 19:35
Show Gist options
  • Save mir4ef/a9c9f7e50a739c60100cf643e759f4b7 to your computer and use it in GitHub Desktop.
Save mir4ef/a9c9f7e50a739c60100cf643e759f4b7 to your computer and use it in GitHub Desktop.
RegExp to validate a date format in JS, but needs extra checks for leap years and 30 day months
// date format: 'YYYY-MM-dd'
var RegExp = /^(199\\d)|([2-9]\\d{3})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
// date format: 'MM/dd/YYYY',
RegExp = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
// passed as a string
var regexp = new RegExp('^(0[1-9]|1[0-2])\/(0[1-9]|1\\d|2\\d|3[01])\/(19|20)\\d{2}$');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment