Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active October 30, 2019 11:28
Show Gist options
  • Save mayeenulislam/c77e4da53bb9ef5d553efa4d153e7e4b to your computer and use it in GitHub Desktop.
Save mayeenulislam/c77e4da53bb9ef5d553efa4d153e7e4b to your computer and use it in GitHub Desktop.
Regular Expressions for HTML5 Patterns

HTML5 Regular Expressions

DateTime (12 Hour format with AM/PM)

([0-2][0-9]|(3)[0-1])(-)(((0)[0-9])|((1)[0-2]))(-)\d{4} (1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)

Example

<input type="text" class="form-control" value="" placeholder="Pick a datetime, e.g., 21-02-1952 12:59 PM" pattern="([0-2][0-9]|(3)[0-1])(-)(((0)[0-9])|((1)[0-2]))(-)\d{4} (1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)">

Valid

  • 17-10-2019 05:24 PM
  • 17-10-2019 05:24 AM
  • 17-10-2019 12:24 am

Invalid

  • 17-10-2019 13:24 am
  • 17-10-2019 13:24

Alternative

Use <input type="datetime-local">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment