Page 1 of 2

User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 5:39 pm
by JAB Creations
I'm trying to put together a regex pattern to determine if a user name uses only alpha-numeric characters and spaces. I already have PHP programmed to determine if there is more then one space, if the user name starts or ends with the space. However if that can be determined with the regex that would be cool too.

Here are some of the things I've kind of been messing with...

Starts with alphabetical character...
^a-zA-Z

Spaces are optional since I have them worked out though I don't mind learning for anyone who wants to add to this...

I'd like to apply this to spaces...have an optional space but must not exceed more than a single instance. (I know a space is a literal, how do I define it then?)
{0,1}

I can determine the length just fine already so I'm not concerned about that.

Valid Matches (encased in double quotes)...
"JAB Creations"
"John Doe"
"Jane Doe123"
"mikewilliams"

Invalid Matches...
"nice day today" (two spaces)
"a .foryou"
"1start withanumber"

Suggestions please?

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 5:54 pm
by VladSun
So ... you don't want your users use usernames which start or end with spaces ...
But if you permit spaces within the username, whhy should you restrict successive spaces?

Code: Select all

^\w+[\w\s\d]*[\w\d]+$

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 6:21 pm
by JAB Creations
Gah...you had to ask that didn't you? :P

In all honestly I can't recall why at this moment in time.

Offhand I think it had something to do with matching "JAB Creations" against "jabcreations" to prevent people duplicating or impersonating accounts. I'll probably remove the space and scan the database for user names without the space. In example my user name will be "JAB Creations" so if some...oh wait that's reverse! :| Talk about a wrench...maybe I'll have a second column where user names stripped of spaces are stored and check against that? Hmm...

I'm not sure what your regex suggestions actually matches? 8O

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 6:27 pm
by VladSun
JAB Creations wrote:Gah...you had to ask that didn't you? :P
Yep ;)
JAB Creations wrote:I'm not sure what your regex suggestions actually matches? 8O
Usernames that:
1) start with a letter;
2) folllowed by a letter, or a number, or a white space character
3) edning with a letter or a number character

PS: http://www.weitz.de/regex-coach/ - you will be happy with that ;)

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 7:21 pm
by JAB Creations
I think my logic is flawed then?

Code: Select all

if (preg_match("/^\w+[\w\s\d]*[\w\d]+$", $username)) {echo "\n".$username." = regex match\n";} else {echo "\n".$username." = regex NOT match\n";}
I'm testing this via AJAX. :mrgreen: I click a button and a JavaScript alert pops up with the response text. I'm absolutely loving this stuff.

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 7:58 pm
by JAB Creations
No no no...I just forgot the ending slash. :|

Thanks, I'm testing this out now. :mrgreen:

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 8:52 pm
by JAB Creations
How does this regex look? :)

Code: Select all

^[a-zA-Z]+[a-zA-Z\s\d]*[a-zA-Z\d]+$
I've been doing a lot of testing and this one does not match periods, dashes, underscores, and also disallows alphabetical characters as the first!

Any suggestions for strings I could try testing it against? Regex is awesome once you get the hang of it! :mrgreen:

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 9:04 pm
by VladSun
Use the link I've given above :)
I'm sure you'll do it yourself ;)
It's high time you've started doing stuff like this by your own ;)

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 9:16 pm
by JAB Creations
NO DON'T LEAVE ME! :cry:

Haha JK! Wow this is faster then AJAX! I like how it highlights the matching *parts* of the string! I'm just blasting through testing now!

What does the tree do? Is this sort of like the top branch is the start/carrot match, the second the general matching, and the bottom branch the end matching?

The step part also helps dramatically! Way freaking cool! This program is so getting a place in my application installer archives! :mrgreen:

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 9:30 pm
by JAB Creations
I'm sort of stuck on * and $.

It seems * includes all the characters after the first including the last. How do I force * to not include the last character or do I want a different operator?

How do I make these two work together more constructively? Am I discussing the middle and ending branches of the tree so to speak? Going through the steps I've tried a few things though can't see to select the last character. :|

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 9:33 pm
by VladSun
* - match previous match repated zero, one or more times;
$ - end of line;

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Sun Sep 14, 2008 9:41 pm
by JAB Creations
Gah...I know that already...

So The Regex Coach has a tab called "tree".

I'm trying to discuss matching in three segments...the first, body, and last segment.

The issue is I'm not sure how to separate the "body" and "last" segments.

Do I put the $ before or after the brackets in example?

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Mon Sep 15, 2008 1:03 am
by JAB Creations
This seems to have very minimal "repeating" in The Regex Coach's step tab.

Code: Select all

^[a-zA-Z][\w\s\d]*[\w\d]$
In example on the string "awe" it only executes 10 steps. :)

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Mon Sep 15, 2008 1:10 am
by JAB Creations
However I can't use the character class abbreviations as it is undesirably matched an ampersand.

Code: Select all

^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]$
Any way I'll keep messing with it...wonder if I can get this down to three steps. :lol:

Re: User name with a-z A-Z 0-9, and spaces?

Posted: Mon Sep 15, 2008 3:37 am
by prometheuzz
JAB Creations wrote:...

Valid Matches (encased in double quotes)...
"JAB Creations"
"John Doe"
"Jane Doe123"
"mikewilliams"

Invalid Matches...
"nice day today" (two spaces)
"a .foryou"
"1start withanumber"

Suggestions please?
This regex will do just that:

Code: Select all

^(?!(\S* ){2})[a-zA-Z][a-zA-Z\d\s]*$