title: Regular Expression to match valid dates
body: <p>I'm trying to write a regular expression that validates a date.  The regex needs to match the following</p>
  
  <ul>
  <li>M/D/YYYY</li>
  <li>MM/DD/YYYY</li>
  <li>Single digit months can start with a leading zero (eg: 03/12/2008)</li>
  <li>Single digit days can start with a leading zero (eg: 3/02/2008)</li>
  <li>CANNOT include February 30 or February 31 (eg: 2/31/2008)</li>
  </ul>
  
  <p>So far I have </p>
  
  <pre><code>^(([1-9]|1[012])[-/.]([1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d)|((1[012]|0[1-9])[-/.](3[01]|2\d|1\d|0[1-9])[-/.](19|20)\d\d)$
  </code></pre>
  
  <p>This matches properly EXCEPT it still includes 2/30/2008 &amp; 2/31/2008.</p>
  
  <p>Does anyone have a better suggestion?</p>
  
  <p><strong>Edit:</strong> I found <a href="http://regexlib.com/REDetails.aspx?regexp_id=112" rel="noreferrer">the answer</a> on RegExLib</p>
  
  <pre><code>^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$
  </code></pre>
  
  <p>It matches all valid months that follow the MM/DD/YYYY format.</p>
  
  <p>Thanks everyone for the help.</p>