Pune
Pune

Reputation: 91

regular expression for date format in javascript

I need a regular expression for date format: mm/yyyy in Javascript.

I try by the following.

var reDate = /^(\d{1,2})/(\d{4})$/;

But not working. I think the problem is in /

Upvotes: 0

Views: 76

Answers (1)

Tushar
Tushar

Reputation: 87233

To use forward slash in regex, it need to be escaped, because it is also used as delimiter.

/^(\d{1,2})\/(\d{4})$/
           ^^

The above regex will also match any two digits, ex. 00, 99. Use following regex to match digits only from 1-12.

Credits to How can I use a regular expression to validate month input?

^(0?[1-9]|1[012])\/[0-9]{4}$

Demo

Upvotes: 5

Related Questions