Jump to content


Photo

Match allowed names


  • Please log in to reply
4 replies to this topic

#1 bearruler

bearruler
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 21 July 2006 - 05:23 PM

I have a little experience with Regular expressions, but this one is just pissing me off
Im sure its so simple, but everything I have tried/found doesnt work

All I need is a simple pattern to validate names on my website. Names can have letters (case doesnt matter, must start in a letter), numbers, underscores, and dashes

Allowed names:
bearruler
bearruler129
bear_ruler
bear-ruler
be-ar_ru13r

Illegal names:
_bearruler
bear ruler
129bearruler
bear@ruler
bearru|er
{bearruler}

You get the point...

Anyway, the php should be
$name="The Name They are attempting to use";
$pattern='thepattern';
if (preg_match($pattern, $name)) echo "ok";
//etc...

Does anyone know a pattern that will fit this?

Thanks,


Bear
www.KingdomsDivided.com

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 July 2006 - 05:26 PM

Have a read of this topic maybe. As its basically the same as what you are doing.

#3 bearruler

bearruler
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 21 July 2006 - 06:00 PM

I tried everything in there, it returns 1 for things like bearruler, but also names like bea$$rruler


Bear
www.KingdomsDivided.com

#4 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 21 July 2006 - 08:56 PM

try this pattern:

^(?=.*?[\d])?(?=.*?[a-zA-Z])(?=.*?[\-\_])?[a-zA-Z][\-\w]{8,25}$/s

note the {8,25} this requires that usernames are at least 8 characters but no more than 25 characters long. Why? I have found that very long usernames cause trouble in the long run. If you want more combinations allow dots(.) or other symbol in your usernames.

To remove this restriction simply replace {8,25} with * or +



#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 21 July 2006 - 09:05 PM

<?php

	### Set up test cases.
	$tests = array(
		### Should pass.
		'bearruler',
		'bearruler129',
		'bear_ruler',
		'bear-ruler',
		'be-ar_ru13r',
		### Should fail.
		'_bearruler',
		'bear ruler',
		'129bearruler',
		'bear@ruler',
		'bearru|er',
		'{bearruler}',
	);
	
	### Run tests.
	foreach ($tests as $test) {
		echo "$test --";
		echo preg_match('/^[a-z][-\w]+$/i', $test) ? 'Pass' : 'Fail';
		echo '<br />';
	}

?>


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users