Validate time format using preg_match

后端 未结 3 1809
离开以前
离开以前 2020-12-11 12:38

I want to validate a time input (like 10:30 am, 02:30 pm) using preg_match()

I use this,

 $pattern   =   \"/([1-12]):([0-5])([0-9])( )(a         


        
3条回答
  •  死守一世寂寞
    2020-12-11 13:06

    The [...] notation in regexes defines character class: something that's applied to the single character. It's alternation that should be used here instead:

    0[1-9]|1[0-2]
    

    ... so the regex in whole would look like this:

    /^(?:0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$/
    

提交回复
热议问题