Jump to content

PCRE - Username Expression


Mko

Recommended Posts

Hey all,

I'm getting ready to launch something, and I've built off a past Regular Expression to limit the characters able to be entered in a username.

Here's my original PCRE:

^[0-9a-zA-Z_ ]*$

 

Now, what I really need to make sure is included in this PCRE is:

-Making sure that any number is allowed (already taken care of by 0-9)

-Making sure that any letter is allowed, including capital (already taken care of by a-zA-Z)

-Making sure that underscores (_), dashes (-), and spaces ( ) are allowed in a Username but CAN NOT BE USED AS THE LAST CHARACTER IN A USERNAME.

So, if anyone would be so kind as to modify my original PCRE to fit my specifications, I'd greatly appreciate it.

 

Adding on to the previous, specifications, here are some lists of allowed/disallowed usernames so you can understand what I'm really trying to get at.

Here's a list of some example usernames that should be ALLOWED using a revised version of this PCRE:

Hello World1

Hello-World1

Hello_World1

 

Here's a list of some example usernames that should be DISALLOWED using a revised version of this PCRE:

HelloWorld1_

HelloWorld1-

HelloWorld1 (note the space after the 1)

 

 

Thanks for any assistance; I appreciate it greatly!

`Mark

Link to comment
Share on other sites

While I don't like sites that enforce such arbitrary limits, you would need two separate character classes since the last character restriction is different from the restriction for all other characters:

 

^[0-9a-zA-Z_ -]*[0-9a-zA-Z]$

 

However, I would think you want to prevent a space as the first character as well:

 

^[0-9a-zA-Z_-][0-9a-zA-Z_ -]*[0-9a-zA-Z]$

 

Oops. That would require (allow) a minimum of two characters (in the second example), since the middle restriction is optional (0 or more).  If you want to force a minimum and/or maximum, you would set a limit on the middle class with the bounds being (in the second case) two characters less than the requirement --- because the first and last must match one character, each.

 

So to force 8 to 20 characters, it would be:

^[0-9a-zA-Z_-][0-9a-zA-Z_ -]{6,18}[0-9a-zA-Z]$

 

Note: I am not an RegExpert, so I'm sure someone else may have a better idea.

Note: This is untested. I leave that as an exercise to the reader.

Link to comment
Share on other sites

While I don't like sites that enforce such arbitrary limits, you would need two separate character classes since the last character restriction is different from the restriction for all other characters:

 

^[0-9a-zA-Z_ -]*[0-9a-zA-Z]$

 

However, I would think you want to prevent a space as the first character as well:

 

^[0-9a-zA-Z_-][0-9a-zA-Z_ -]*[0-9a-zA-Z]$

 

Oops. That would require (allow) a minimum of two characters (in the second example), since the middle restriction is optional (0 or more).  If you want to force a minimum and/or maximum, you would set a limit on the middle class with the bounds being (in the second case) two characters less than the requirement --- because the first and last must match one character, each.

 

So to force 8 to 20 characters, it would be:

^[0-9a-zA-Z_-][0-9a-zA-Z_ -]{6,18}[0-9a-zA-Z]$

 

Note: I am not an RegExpert, so I'm sure someone else may have a better idea.

Note: This is untested. I leave that as an exercise to the reader.

 

Thanks! I was able to do it like this:

^[0-9a-zA-Z]+[0-9a-zA-Z_ -]*[0-9a-zA-Z]+$

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.