0

I am trying to write a regex expressions in PHP to validate two variables:

1). $months range from (1 - 31)

2). $days range from (1 - 12)

How could you write that regex expression for each ?

Thanks

user3150060
  • 1,725
  • 7
  • 26
  • 46
  • Regular expressions for checking number ranges is a bad idea. Your best bet is to cast the value *as a number* then use an `if` statement to validate it. – Mr. Llama Jun 03 '14 at 21:00
  • In addition to all the other answers, that are telling you the smart way to take care of this (using logic available in PHP)..check out [my other answer](http://stackoverflow.com/questions/23637637/regex-for-checking-if-number-is-less-or-greater-than/23637810#23637810) as to why its not easy to evaluate ranges in regex. – Sam Jun 03 '14 at 21:03
  • 1
    Well this is interesting. Do you need exact validation (the given date exists with certainty) or just rough validation? With rough validation I mean that is it okay that some individual dates get passed as false positives? – jsalonen Jun 03 '14 at 21:03
  • 1
    You might could do it like they do here http://stackoverflow.com/questions/1578628/is-there-a-regular-expression-for-a-comma-separated-list-of-discrete-values by listing every day of the month with the `|` operator, but what a hassle. – developerwjk Jun 03 '14 at 21:03
  • 1
    Since when are there 31 months in a year, or only 12 days in a month? – Marc B Jun 03 '14 at 21:10

1 Answers1

0

You would be better off using simple php opperators,e.g.

if( $months > 0 && $months < 32 )

But the issue you will have is some months only have some many days. Use this instead:

http://www.php.net/manual/en/function.checkdate.php

Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48