Reputation: 91
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
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}$
Upvotes: 5