Reputation: 63
I have to make an HTML input pattern that accepts only date and 12hrs time in the format yyyy-mm-dd hh:mm AM/PM so far i've come up with this expression
<input type='text' name='a' pattern='[0-9]{4}-[0-1][0-9]-[0-3][0-9] (2[012]|[1-9]):[0-5][0-9] (am|pm|AM|PM)' required>
This accpets the input as 2016-12-22 1:50 AM i want is to accept 2 digits for the time like 01,06,12 etc. Is this Possible ?
Upvotes: 0
Views: 2026
Reputation: 33273
First, it should be 1[012]
instead of 2[012]
(Hour = 10, 11, 12 and not 20, 21, 22):
pattern='[0-9]{4}-[0-1][0-9]-[0-3][0-9] (1[012]|[1-9]):[0-5][0-9] (am|pm|AM|PM)'
To make it require a 0
:
pattern='[0-9]{4}-[0-1][0-9]-[0-3][0-9] (1[012]|0[1-9]):[0-5][0-9] (am|pm|AM|PM)'
To make the 0
optional:
pattern='[0-9]{4}-[0-1][0-9]-[0-3][0-9] (1[012]|0?[1-9]):[0-5][0-9] (am|pm|AM|PM)'
Upvotes: 1