Reputation: 5969
preg_match_all("/[^A-Za-z0-9]/",$new_password,$out);
The above only checks the 1st character, how to check whether all are alpha-numeric?
Upvotes: 6
Views: 18117
Reputation: 43263
It's probably a better idea to use the builtin functions: ctype_alnum
Upvotes: 23
Reputation: 3776
Old question, but this is my solution:
<?php
public function alphanum($string){
if(function_exists('ctype_alnum')){
$return = ctype_alnum($string);
}else{
$return = preg_match('/^[a-z0-9]+$/i', $string) > 0;
}
return $return;
}
?>
Upvotes: 2
Reputation: 97845
preg_match("/^[A-Za-z0-9]*$/", $new_password);
This gives true
if all characters are alphanumeric (but beware of non-english characters). ^
marks the start of the string, and ^$^ marks the end. It also gives true
if the string is empty. If you require that the string not be empty, you can use the +
quantifier instead of *
:
preg_match("/^[A-Za-z0-9]+$/", $new_password);
Upvotes: 8