Skip to content

Instantly share code, notes, and snippets.

@amandasaffer
Created August 24, 2019 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandasaffer/37ef3926e2bb015e69358423f926ff66 to your computer and use it in GitHub Desktop.
Save amandasaffer/37ef3926e2bb015e69358423f926ff66 to your computer and use it in GitHub Desktop.
mm/dd/yyyy regex
<input type="text" id="date" placeholder="mm/dd/yyyy">
<button class="pressy">validate</button>
/* following this stack overflow answer: https://stackoverflow.com/questions/15196451/regular-expression-to-validate-datetime-format-mm-dd-yyyy
*/
function validateDate(thedate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(thedate);
}
document.addEventListener('click', function (event) {
if (!event.target.matches('.pressy')) return;
event.preventDefault();
console.log(validateDate(document.getElementById("date").value));
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment