Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing JS regex for YYYY-MM-DD HH:MM [duplicate]

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

like image 491
user3355028 Avatar asked Nov 30 '22 18:11

user3355028


1 Answers

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.
}

enter image description here

SEE DEMO :https://www.regex101.com/r/rT6vJ4/1

like image 59
Always Sunny Avatar answered Dec 04 '22 11:12

Always Sunny