Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$
. The ^
marks the beginning of the string, $
the end. That makes sure, that only four numbers are valid, and nothing before or after that.