문제

I need a pattern that verifies the input field is in the following format.

1st char: a, b, c, or d
2nd: 0-9
third: 0-9
4th: 0-9 (optional char)

valid example: a939, c321, d11, c32
invalid: aa939, c1

preg_match('/^[DSFP][0-9]/', $data) // doesn't work :(
도움이 되었습니까?

해결책

Your regex should be:

/^[abcd]\d{2,3}$/i

Explanation:

^       -- matches line start
[abcd]  -- matches one of literal characters a, b, c, d
\d{2,3} -- matches 2 or 3 digits
$       -- matches line end

다른 팁

Just try with following regex:

/^[a-d][0-9]{2,3}$/

Explanation:

[a-d]      - allows letters from `a` to `d`
[0-9]      - allows just one digit - so invalid here
[0-9]{2,3} - allows 2 to 3 digits
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top