HTML5 pattern for formatting input box to take date mm/dd/yyyy?

MJ X picture MJ X · Jun 13, 2013 · Viewed 151.7k times · Source

I used jQuery datepicker and I want to restrict my input field with a HTML5 pattern if a user, instead of getting the date from jQuery datepicker, types the date.

How can I restrict my users with a HTML5 pattern so that they can just type the date in the form mm/dd/yyyy?

Answer

Stanislav Terletskyi picture Stanislav Terletskyi · Jun 13, 2013

Easiest way is use read only attribute to prevent direct user input:

 <input class="datepicker" type="text" name="date" value="" readonly />

Or you could use HTML5 validation based on pattern attribute. Date input pattern (dd/mm/yyyy or mm/dd/yyyy):

<input type="text" pattern="\d{1,2}/\d{1,2}/\d{4}" class="datepicker" name="date" value="" />