User name with a-z A-Z 0-9, and spaces?
Moderator: General Moderators
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
User name with a-z A-Z 0-9, and spaces?
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?
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?
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?
But if you permit spaces within the username, whhy should you restrict successive spaces?
Code: Select all
^\w+[\w\s\d]*[\w\d]+$There are 10 types of people in this world, those who understand binary and those who don't
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
Gah...you had to ask that didn't you? 
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?
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!
I'm not sure what your regex suggestions actually matches?
Re: User name with a-z A-Z 0-9, and spaces?
YepJAB Creations wrote:Gah...you had to ask that didn't you?
Usernames that:JAB Creations wrote:I'm not sure what your regex suggestions actually matches?
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
There are 10 types of people in this world, those who understand binary and those who don't
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
I think my logic is flawed then?
I'm testing this via AJAX.
I click a button and a JavaScript alert pops up with the response text. I'm absolutely loving this stuff.
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";}- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
No no no...I just forgot the ending slash. 
Thanks, I'm testing this out now.
Thanks, I'm testing this out now.
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
How does this regex look? 
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!
Code: Select all
^[a-zA-Z]+[a-zA-Z\s\d]*[a-zA-Z\d]+$Any suggestions for strings I could try testing it against? Regex is awesome once you get the hang of it!
Re: User name with a-z A-Z 0-9, and spaces?
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
I'm sure you'll do it yourself
It's high time you've started doing stuff like this by your own
There are 10 types of people in this world, those who understand binary and those who don't
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
NO DON'T LEAVE ME! 
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!
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!
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
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.
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?
* - match previous match repated zero, one or more times;
$ - end of line;
$ - end of line;
There are 10 types of people in this world, those who understand binary and those who don't
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
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?
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?
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
This seems to have very minimal "repeating" in The Regex Coach's step tab.
In example on the string "awe" it only executes 10 steps. 
Code: Select all
^[a-zA-Z][\w\s\d]*[\w\d]$- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: User name with a-z A-Z 0-9, and spaces?
However I can't use the character class abbreviations as it is undesirably matched an ampersand.
Any way I'll keep messing with it...wonder if I can get this down to three steps. 
Code: Select all
^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]$- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: User name with a-z A-Z 0-9, and spaces?
This regex will do just that: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?
Code: Select all
^(?!(\S* ){2})[a-zA-Z][a-zA-Z\d\s]*$