Goldeneye Posted July 20, 2008 Share Posted July 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/ Share on other sites More sharing options...
Third_Degree Posted July 20, 2008 Share Posted July 20, 2008 Edit: Oh sorry, didn't read your post thoroughly enough, hold on I'll get code back to you in a second. Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-594702 Share on other sites More sharing options...
corbin Posted July 20, 2008 Share Posted July 20, 2008 if(preg_match('/^[\w_\. -]{4,16}$/', $username) && !preg_match('/[ ]{2,}/', $username)) { } Should work, but someone else might be able to get it into 1 regexp.... Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-594948 Share on other sites More sharing options...
Goldeneye Posted July 20, 2008 Author Share Posted July 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-594972 Share on other sites More sharing options...
Goldeneye Posted July 21, 2008 Author Share Posted July 21, 2008 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-595154 Share on other sites More sharing options...
corbin Posted July 21, 2008 Share Posted July 21, 2008 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) { ) Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-595651 Share on other sites More sharing options...
Goldeneye Posted July 24, 2008 Author Share Posted July 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/115677-solved-a-complex-regular-expression-for-usernames/#findComment-598118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.