Forum Moderators: coopster

Message Too Old, No Replies

simple pregmatch

         

Sandro87

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

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

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

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

WebmasterWorld Senior Member 10+ 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

astupidname

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

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

Sandro87

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

10+ Year Member



thank you!