Jump to content

[SOLVED] A complex Regular Expression for usernames


Goldeneye

Recommended Posts

Hello,

 

I'm tweaking my PHP user-registration script for a site/forum of mine and I've been trying to write a Regex that'll allow the following parameters to be used as a username:

White-Spaces (non-consecutive)

Alpha-Numeric

And miscellaneous characters (_ - .)

With a minimum character limit of 4 and a maximum character limit of 16.

 

So matches would include:

Foobar
Foo bar
Foo_bar
F-o o_b.ar
1F-o2 o_b.3ar4

 

And non-matches would include:

Foo    bar
$Foobar
Foo    %#@

 

I've searched a few sites and tried putting one together, but success wasn't going to come. So how would one go about creating a Regular Expression (or two) that'll match these parameters?

Link to comment
Share on other sites

Well two Regular Expressions is good for me. I was trying to get it into a single one, but it just wouldn't co-operate with me. Anyways, thanks a lot! I'll have to wait to test this, though, as my website seems to be giving a 503 error at the moment.

Link to comment
Share on other sites

Hmmm it doesn't appear to be working.. Here's how I'm using it..

<?php
if(!preg_match('/^[\w_\. -]{4,16}$/', $_POST['pseudonym']) && preg_match('/[ ]{2,}/', $_POST['pseudonym'])) {
	echo '<p class="errmessage">Usernames are to be between 4 and 16 characters with numbers, non-consecutive-whitespaces, letters, and a few miscellaneous characters.</p>';
}
?>

Link to comment
Share on other sites

It should be

 

if(preg_match('/^[\w_\. -]{4,16}$/', $_POST['pseudonym']) && !preg_match('/[ ]{2,}/', $_POST['pseudonym'])) {

 

 

If we go through the two patterns, this is the first one:

Starting from the beginning (^ thing),

\w (meaning word characters.... alphanumeric basically) _ .  "" or - repeated 4 to 16 times

String ends

 

Second pattern:

Space repeated twice anywhere in the string.

 

So, you would want the first to match, and the second to not match.

 

 

(If you want to avoid regexp on the second you could do

 

if(preg_match('/^[\w_\. -]{4,16}$/', $_POST['pseudonym']) && strpos($_POST['pseudonym'], "  ") === false) {

 

)

Link to comment
Share on other sites

Nevermind, I got it figured out. I wanted it to return a message if the user-input didn't match the Regex. So I just changed:

 

<?php
if(!preg_match('/^[\w_\. -]{4,16}$/', $_POST['pseudonym']) && preg_match('/[ ]{2,}/', $_POST['pseudonym'])) {
	echo '<p class="errmessage">Usernames are to be between 4 and 16 characters with numbers, non-consecutive-whitespaces, letters, and a few miscellaneous characters.</p>';
}
?>

 

To...

 

<?php
if(!preg_match('/^[\w_\. -]{4,16}$/', $_POST['pseudonym']) || preg_match('/[ ]{2,}/', $_POST['pseudonym'])) {
	echo '<p class="errmessage">Usernames are to be between 4 and 16 characters with numbers, non-consecutive-whitespaces, letters, and a few miscellaneous characters.</p>';
}
?>

 

I made it so it says "If no PREG_MATCH() for first Regex OR is PREG_MATCH() for second Regex, echo..." That way, it doesn't require that both parameters are met to be able to display the error message.

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.