Jump to content


Photo

Alphanumerical username with underscores and spaces


  • Please log in to reply
8 replies to this topic

#1 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 01 July 2006 - 06:09 PM

What would be the pattern for this?

Allowed charactors: aA-zZ 0-9 _ space

Thanks in advance!

---brandon
PHP is my friend (=

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 01 July 2006 - 06:47 PM

[^a-zA-Z0-9 _]
imo

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 01 July 2006 - 07:29 PM

Okay, so that worked before I integrated it within a function.

Here's my test code:
<?php
	if(isset($_POST["submit"]))
		echo username_valid_test($_POST["username"]);

	echo "<br \>";
	echo "<form method=\"post\" action=\"" . $_SERVER["PHP_SELF"] . "\">";
	echo "<input type=\"text\" name=\"username\">";
	echo "<br \><input type=\"submit\" name=\"submit\">";
	echo "</form>";

	function username_valid_test($username)
	{
		$len = strlen($username);

		if($username{0} == ' ' OR $username{0} == '_')
			return $username . " - Error #1";
		elseif(!eregi("[^a-zA-Z0-9 _]{3,16}" , $username))
			return $username . " - Error #2";
		elseif(strpos($username , "  ") !== FALSE)
			return $username . " - Error #3";
		elseif($username{$len - 1} == '_' OR $username{$len - 1} == ' ')
			return $username . " - Error #4";

		return $username . " valid";
	}
?>
- and here's the URL for the page that contains that exact code above: http://ghw-amxx.com/...rname_regex.php

In the past I've experienced problems with $_POST data and regex, but I don't think that's the case here. Also, it doesn't work without the {3,16} part either.

---brandon
PHP is my friend (=

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 02 July 2006 - 08:22 AM

Try "[^a-zA-Z0-9 \_]{3,16}"
Maybe the underscore needs to be escaped.

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 02 July 2006 - 10:09 AM

eregi doesn't like me :|

I edited a pattern and I got it working. Here's the code for anyone for future reference:
<?php
	if(isset($_POST["submit"]))
		echo username_valid_test($_POST["username"]) . "<br \>";

	echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"post\">";
	echo "<input type=\"text\" name=\"username\">";
	echo "<br \><input type=\"submit\" name=\"submit\">";
	echo "</form>";

	function username_valid_test($username)
	{
		$len = strlen($username);

		if(!preg_match("/^[a-zA-Z0-9 _]*$/" , $username)) {
			return $username . " - Error #1";
		}
		elseif($username{0} == ' ' OR $username{0} == '_') {
			return $username . " - Error #2";
		}
		elseif($username{$len - 1} == '_' OR $username{$len - 1} == ' ') {
			return $username . " - Error #3";
		}
		elseif(strpos($username , "  ") !== FALSE) {
			return $username . " - Error #4";
		}

		return $username . " valid";
	}
?>

PHP is my friend (=

#6 Catzwolf

Catzwolf
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 July 2006 - 07:54 PM

Try this instead:

'/[^a-zA-Z0-9\_\-]/'


#7 effigy

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

Posted 08 July 2006 - 07:58 PM

FYI: [a-zA-Z0-9_] is equivalent to \w when using preg.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 08 July 2006 - 10:26 PM

Using ^ inside a character set negates all the characters, I.E. you are saying all except a-zA-Z0-9_ if you have ^ at the start.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#9 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 10 July 2006 - 03:59 PM

you don't have to escape underscores and dashes if they are the leading characters in an expression.
you can also use the i modifier to make expressions case insensitive

'/[^a-zA-Z0-9\_\-]/'
is equivalent to
'/[^-_a-zA-Z0-9]/'
which is, in turn, equivalent to
'/[^-_a-z0-9]/i'





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users