mardi 5 mars 2019

regex pattern for datetime in html5, conforming to sql datetime(3) (24hr, and milliseconds)

I searched for html5 regex pattern to match sql datetime(3) datatype, such as

"2019-2-31 4:3:21.586", but also handling the leading zeroes, as in "2019-02-31 04:03:21.586", AND where the ".586" (milliseconds) is optional, as in "2019-02-31 04:3:21"

I couldn't find one. So I wrote one, and am sharing here, to save you time:

"[\d]{4}-(1[0-2]|0[1-9]|[1-9])-(3[01]|[12][0-9]|0[1-9]|[1-9]) (2[0-4]|1[0-9]|0[1-9]|[1-9]):([0]|[1-5])?[0-9]:([0]|[1-5])?[0-9](.[\d]{1,3})?"

HTML example:

<input type="text" name="dateFrom" id="dateFrom"
value="2019-02-31 04:03:21.586" required 
pattern="[\d]{4}-(1[0-2]|0[1-9]|[1-9])-(3[01]|[12][0-9]|0[1-9]|[1-9]) (2[0-4]|1[0-9]|0[1-9]|[1-9]):([0]|[1-5])?[0-9]:([0]|[1-5])?[0-9](.[\d]{1,3})?">

Tested on Safari and Chrome. Hope it helps (apologies if I missed a post that has it).

Aucun commentaire:

Enregistrer un commentaire