preg_match("/^[A-Za-z0-9]*$/", $new_password);
This gives trueif all characters are alphanumeric (but beware of non-English characters). ^denotes the beginning of a line, and ^ $ ^ denotes the end. It also gives trueif the string is empty. If you want the string not to be empty, you can use a quantifier +instead *:
preg_match("/^[A-Za-z0-9]+$/", $new_password);
source
share