Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

simple pregmatch



1:27 pm on Apr 11, 2010 (gmt 0)

5+ Year Member

This is stupid I know, but I can't find a way to do it, I know the expressions but I don't know where and how to set them..

I have

if (!preg_match ("/([a-z0-9._]+)+$/", trim($_POST["username"]))){



I need to verify that "username" has only upper/lowercase chars with "." and "_" allowed. but I want also to NOT allow whitespaces in the username

I tried the followings with no success

/([a-z0-9._^ ]+)+$/

Thank you


2:01 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

preg_match('/^[a-z0-9_\.]+$/i', $someVariable)

Not stupid at all by the way, I used to have terrible trouble with regular expressions


3:52 pm on Apr 11, 2010 (gmt 0)

5+ Year Member

Even simpler, \w matches all alpha-numeric characters (case-insensitive) and the underscore (equivalent of [a-zA-Z0-9_]):
preg_match("/^[\w\.]+$/", $someVariable)

So, actually if you want to check the variable does not contain anything other than those allowed characters, you could do:
if (!preg_match("/[^\w\.]/", $someVariable)) {
//do something, $someVariable only contains the allowed characters

Remember that the '^' character when used un-escaped inside of character class brackets excludes the given characters and checks for anything other than those characters given, so the latter check checks for anything other than \w or \.


2:12 pm on Apr 27, 2010 (gmt 0)

5+ Year Member

thank you!

Featured Threads

Hot Threads This Week

Hot Threads This Month