I'm new with JS and I need to validate a date in a form. The date field is a regular text field. The name of the date field called "date". How can I validate if the date is from the format of YYYY-MM-DD HH:MM with a simple regular expression in JS?
Thanks
Try this way to validate your date in YYYY-MM-DD HH:MM
format
var re = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]/;
var str = '2014-02-04 12:34';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
SEE DEMO :https://www.regex101.com/r/rT6vJ4/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With