A Pen by Amanda Saffer on CodePen.
Created
August 24, 2019 22:42
-
-
Save amandasaffer/37ef3926e2bb015e69358423f926ff66 to your computer and use it in GitHub Desktop.
mm/dd/yyyy regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" id="date" placeholder="mm/dd/yyyy"> | |
<button class="pressy">validate</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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