There are two ways to do this.
Indicate whether the variable contains any one character, and not in the allowed ranges. This is achieved using the negative character class [^ ...]:
preg_match('/[^a-zA-Z0-9\.]/', $your_variable);
Another alternative is to make sure that each character in the string is in the allowed range:
!preg_match('/^[a-zA-Z0-9\.]*$/', $your_variable);
source
share