Skip to content

Instantly share code, notes, and snippets.

@pmakholm
Created July 29, 2009 17:50
Show Gist options
  • Save pmakholm/158292 to your computer and use it in GitHub Desktop.
Save pmakholm/158292 to your computer and use it in GitHub Desktop.
Regular expression to validate dates on the forms
#!/usr/bin/perl -ln
# Regular expression to validate dates on the forms 'YYYY-MM-DD' or 'YYYYMMDD'
$re = qr/(((0[48]|[2468][048]|[13579][26])00| \d\d(0[48]|[2468][048]|[13579][26]
))|(( [02468] [1235679]| [13579] [01345789])00 |\d\d([02468] [1235679]|
[13579][01345789]))(?!(-|)02\g{-1}29)) (?<sep>-|)(02(?!\g{sep}3)|0[469]
(?!\g{sep}31)|11(?!\g{sep} 31)|0[13578]|1[02])\g{sep}(0[1-9]|[12][0-9]|
3[01])/x;
print ($_=~ $re ? "Valid date" : "Invalid date");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment