Validate Month Year Format

2020-05-07 08:11发布

I need to validate Text box in this format (ex:FEB 2014 MMM YYYY).

I am using the following regular expression string

^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\-\d{4}$

Only issue is that my input is with a 'space' and not '-' i.e. JUN 2012 not JUN-2012

Can someone please amend the above regex to cater for space

Thanks

2条回答
Deceive 欺骗
2楼-- · 2020-05-07 08:30

Try the below regex to match month and year in this MMM YYYY format ,

^(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) \d{4}$

DEMO

查看更多
\"骚年 ilove
3楼-- · 2020-05-07 08:43

use \s instead of \- in your regex

like this :

^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s\d{4}$

@Avinash is right \s matches [\r\n\t\f] better use " " instead.

查看更多
登录 后发表回答