Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This will handle actual dates and give you the chance to find what part of the date was invalid - using the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date" rel="nofollow">DATE OBJECT</a></p> <p><strong>NOTE</strong>: several browsers will happily parse what seems to be an invalid date and make a date object out of it. For example 02/29/2013 will parse as 1st of March 2013, hence my test to see if the parts entered made sense when used in an actual date.</p> <p><a href="http://jsfiddle.net/mplungjan/4AKrB/" rel="nofollow">DEMO</a></p> <p>Tested in </p> <blockquote> <p>Win7: </p> <ul> <li>Chrome 23 (only one to give isNaN on the first date)</li> <li>IE 9 </li> </ul> <p>Win XP:</p> <ul> <li>FX 17 </li> <li>IE 8 </li> <li>Safari 5</li> <li>Opera 11 and 12 </li> </ul> </blockquote> <pre><code>function isValidDateCheck(dString) { // test it is nn/nn/nnnn or nn/nn/nn var dRe = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/ if (!dRe.exec(dString)) { return false; } // make sure it parses as date // replace this part if you do not allow dashes dString.replace(/-/g,"/"); var date = new Date(dString); // create a date object if (!isNaN(date)) { // it may give NaN - if not test the parts var parts = dString.split("/"); // split on slash var dd = parseInt(parts[1],10); // day number var mm = parseInt(parts[0],10)-1; // month - JS months start at 0 var yyyy = parseInt(parts[2],10); // year // return true if all parts match return dd===date.getDate() &amp;&amp; mm === date.getMonth() &amp;&amp; yyyy===date.getFullYear(); } // here the date was not parsed as a date return false; } window.onload=function() { document.getElementById("output").innerHTML+="&lt;br/&gt;12/33/2012: "+isValidDateCheck('12/33/2012'); document.getElementById("output").innerHTML+="&lt;br/&gt;12/12/2012: "+isValidDateCheck('12/12/2012') ; document.getElementById("output").innerHTML+="&lt;br/&gt;02/29/2012: "+isValidDateCheck('02/29/2012') ; document.getElementById("output").innerHTML+="&lt;br/&gt;02/29/2013: "+isValidDateCheck('02/29/2013') ; document.getElementById("output").innerHTML+="&lt;br/&gt;01/01/2013A: "+isValidDateCheck('01/01/2013A') ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload