Simple year format validation using PHP -
i validate if given year input in specific format or not. format of year should yyyyad
or yyyybc
(eg: 2013ad
) - 4 numbers , ad/bc without space in between. should return true if input in correct format, else false should returned. (some expected incorrect formats are; 123ad
, xyzad
, ad2013
, ad
, 2013
, @123ad
, 2013ad
). how can achieve this?
thanks in advance...:)
use regex expression such as/^\d{4}(ad|bc)$/
.
explanation of regex:
^
- start of string\d{4}
- digit 0 9 repeated 4 times(ad|bc)
- group of either string "ad
" or string "bc
"$
- end of string
code:
$input = "2013bc"; if (preg_match("/^(\d{4})(ad|bc)$/", $input, $matches)) { echo "ok.\n"; echo "year: " . $matches[1] . "\n"; echo "ac/bc: " . $matches[2]; } else { echo "not ok!"; }
Comments
Post a Comment