Jump to content

a-Z 0-9 -_ characters only *SOLVED*


JackJack

Recommended Posts

use preg_match

[code]<?php

$username = "wildteend"; // returns TRUE - valid

//$username = "wildteen88, Dan"; // returns False - invalid

// check that username only containers characters from a-z in lower/upper case
// and that the string only includes _ and - too
if(preg_match("/^[_a-zA-Z0-9-]+$/", $username))
{
    echo "You have a valid username!"; //TRUE
}
else
{
    echo "You have an invalid username!";  //FALSE
}

?>[/code]

[b]EDIT: Forgot to add in 0-9 in the expression. Thanks 448191 for spotting that out.

Also sorted out a few problems too[/b]
wildteen88, you don't need to backslash the dash in the character class when it's at the beginning or the end. Example:

/[-a-zA-Z_]$/ or /[-a-zA-Z_-]$/ is fine.

You forgot the 0-9 and the fact you can have more than one character. So, this expression would work where it expects at least one character/digit entered:

/^[\w_-]+$/i


Some flavors of regex engines may make \w to mean certain things. To be specific you could do the usual:

/^[a-zA-Z0-9_-]+$/

Edit:

wildteen88 fixed the 0-9 but still missing ^ and a + or * otherwise it's just going to match one character - the last character since there's no ^ and there's a $.
[!--quoteo(post=373773:date=May 14 2006, 11:44 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 14 2006, 11:44 AM) [snapback]373773[/snapback][/div][div class=\'quotemain\'][!--quotec--]
wildteen88 fixed the 0-9 but still missing ^ and a + or * otherwise it's just going to match one character - the last character since there's no ^ and there's a $.
[/quote]

LOL, I missed that...
Thanks toplay. I noticed that my expression wasn't working when I added in 0-9 and somei nvalud characters so I made a quick dash over to php.net and did a quick readup on a few pattern modifiers

I have fixed my expression now. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]
Hey, np.

JackJack, if you want to insure the username starts with a letter, then use an expression something like this:

/^[a-z][a-z0-9_-]*$/i

[img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]
What do you mean? the code that wildteen88 posted includes if statements to handle the response of the preg_match.

[code]<?php

$username = "wildteend"; // returns TRUE - valid

//$username = "wildteen88, Dan"; // returns False - invalid

// check that username only containers characters from a-z in lower/upper case
// and that the string only includes _ and - too
if(preg_match("/^[_a-zA-Z0-9-]+$/", $username))
{
    echo "You have a valid username!"; //TRUE
}
else
{
    echo "You have an invalid username!";  //FALSE
}

?>[/code]

Just change these parts:

echo "You have a valid username!"; //TRUE
...
echo "You have an invalid username!"; //FALSE

I wish it were that simple.

Heres the chunk of php im using.

[code]}elseif(preg_match("/^[_a-zA-Z0-9-]+$/", $_POST['username']))
{
  echo '
    <font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
         • Invalid username: a-Z 0-9 -_ characters only
    </font>';
  $show_form = 1;
}[/code]

$username = "test" would display error
$username = "$}~##" wont display error

:S
[!--quoteo(post=373808:date=May 14 2006, 11:16 AM:name=JackJack)--][div class=\'quotetop\']QUOTE(JackJack @ May 14 2006, 11:16 AM) [snapback]373808[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I wish it were that simple.

Heres the chunk of php im using.

[code]}elseif(preg_match("/^[_a-zA-Z0-9-]+$/", $_POST['username']))
{
  echo '
    <font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
         • Invalid username: a-Z 0-9 -_ characters only
    </font>';
  $show_form = 1;
}[/code]

$username = "test" would display error
$username = "$}~##" wont display error

:S
[/quote]
Put a ! before preg_match. Example:

}elseif(!preg_match...

Read up on basic logic and comparison operators.

[a href=\"http://us2.php.net/manual/en/language.operators.logical.php\" target=\"_blank\"]http://us2.php.net/manual/en/language.operators.logical.php[/a]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.