0

I constructed a regex to match datetimes in the format dd.mm.yyyy hh:ii. This works great on regex tester: https://regex101.com/r/AX4nxj/1 But when used in the pattern attribute of an input field it does not validate at all. You can even type just "a" and it will submit.

<input type="text" required="required" pattern="(?:(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))\.(?:0[1-9]|1[0-2])\.(?:19|20)[0-9]{2}\ (?:[01]\d|2[0-3]):[0-5]\d" name="formName" />

You can try it yourself: http://jsfiddle.net/h2uwhL70/

Why is that? :( Does HTML use a different regex format or what?

1
  • The only error is the backslash before a space. Commented Jul 17, 2017 at 18:23

1 Answer 1

1

I think your regex is a little bit verborage and wrong :D. So try this:

 ^(0+[1-9]|[1-2]+[0-9]|3+[0-1]).(0+[1-9]|1+[0-2]).\d{4} (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$
Sign up to request clarification or add additional context in comments.

5 Comments

maybe just flip the \d\d\d\d to \d{4} but I like it!
thank you. i don't know much about regex but that does not look right and also does not work (on both regex tester and html pattern).
I mean for example why are there three ":"? what is this supposed to match
sniperd, indeed \d{4} is a way more better, thanks for the help. @grindit, the three ":" is my mistake, I didn't realize that you wanna "." instead of ":". Now I edited the answer. Try it again.
but the regex you suggested does also allow d.m.yyyy h:i (single digit). I managed to correct it by myself with my noobie knowledge. also changed yyyy to yy. ^(0?[1-9]|[12][0-9]|3[01]){2}.(0?[1-9]|1[0-2]).\d{2} (00|[0-9]|1[0-9]|2[0-3]){2}:([0-9]|[0-5][0-9]){2}$ just the month part does not work with {2}. can you help me with it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.