homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
simple pregmatch
Sandro87




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

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"]))){

dosomething

}

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._^\s]+)+$/
/([a-z0-9._\S]+)+$/
/([a-z0-9._^ ]+)+$/

Thank you

 

Readie




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

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

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

astupidname




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

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 \.

Sandro87




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

thank you!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved