Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex to validate dateformat from url and otherwise

I need a regex, that validates this 2 patterns:

yyyy-MM-dd HH:mm:ss

and

yyyy-MM-dd%20HH:mm:ss

the second one is for Get parameters of a url.. thats why there is a %20 in there.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit:

I tired this one:

^\d{4}\/([0]\d|[1][0-2])\/([0-2]\d|[3][0-1])(\s([0]\d|[1][0-2])(:[0-5]\d){1,2})*\s*([aApP][mM]{0,2})?$

but it does not work

>Solution :

[0-9]{4}-[0-9]{2}-[0-9]{2}(?:%20| )[0-9]{2}:[0-9]{2}:[0-9]{2}

This’ll work if you’re in JavaScript.

You can also place the separate parts in capture groups and check them if you need to validate e.g. that month is less than 12

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading