Page 1 of 1

how to check for alpha caracters

Posted: Thu Jul 26, 2007 10:00 am
by aceconcepts
Hi,

What pattern would i use to ensure that the only characters entered into a field are alpha characters?

e.g.

"hello" = good

"h3llo" = bad

"he//o" = bad

I hope you like my example :D

Posted: Thu Jul 26, 2007 10:05 am
by TheMoose

Code: Select all

preg_match_all("/\w+/", $string, $matches);

Posted: Thu Jul 26, 2007 10:17 am
by stereofrog

Code: Select all

if(preg_match('/^[a-z]+$/Di', $field))
   echo "ok";
else
   echo "not ok";

Posted: Thu Jul 26, 2007 10:22 am
by aceconcepts
Superb, thanks a lot chaps.

Posted: Thu Jul 26, 2007 11:54 am
by superdezign
ctype_alpha() would be a better choice.

@TheMoose: That wouldn't work. \w matches more than just letters.

Posted: Thu Jul 26, 2007 2:17 pm
by Chalks
Yep, here's feyd's response to the w+ thing:

viewtopic.php?p=245010#245010

Posted: Thu Jul 26, 2007 3:51 pm
by stereofrog
\w and ctype_xxx functions are locale-aware, they both match what is considered a letter or digit in the current locale. Note that locale is not the same as alphabet, e.g "en-US" locale still includes ä á ß and other "european" characters.

Posted: Thu Jul 26, 2007 5:51 pm
by superdezign
stereofrog wrote:\w and ctype_xxx functions are locale-aware, they both match what is considered a letter or digit in the current locale. Note that locale is not the same as alphabet, e.g "en-US" locale still includes ä á ß and other "european" characters.
Ahh, yes, it does say that in the documentation. I'll take note of it.