Michael Kniskern
Michael Kniskern

Reputation: 25270

Regular expression for validating month

What is the regular expression for validating a month with the leading zero?

Passes regular expression:

01,02,03,04,05,06,07,08,09,10,11,12

Fails regular expression:

1, 00, 13 and up.

Upvotes: 4

Views: 9377

Answers (4)

gosku
gosku

Reputation: 23

I think this is a better one, it accepts '05', but also '5' as a month:

/(^0?[1-9]$)|(^1[0-2]$)/

Upvotes: 2

Ali Shah Ahmed
Ali Shah Ahmed

Reputation: 3333

this will work

/(0[1-9])|(1[012])/

Upvotes: -1

Daniel Trebbien
Daniel Trebbien

Reputation: 39208

/^01|02|03|04|05|06|07|08|09|10|11|12$/

Upvotes: 3

Seb
Seb

Reputation: 25147

/^(0[1-9]|1[0-2])$/

Upvotes: 21

Related Questions