how to check for alpha caracters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

how to check for alpha caracters

Post 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
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Code: Select all

preg_match_all("/\w+/", $string, $matches);
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Code: Select all

if(preg_match('/^[a-z]+$/Di', $field))
   echo "ok";
else
   echo "not ok";
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Superb, thanks a lot chaps.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ctype_alpha() would be a better choice.

@TheMoose: That wouldn't work. \w matches more than just letters.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

Yep, here's feyd's response to the w+ thing:

viewtopic.php?p=245010#245010
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply