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]
Link to comment
Share on other sites

Umm, I must have misread the thread. Any whay I've added in 0-9 in to the regular expression now [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]
Link to comment
Share on other sites

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 $.
Link to comment
Share on other sites

[!--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...
Link to comment
Share on other sites

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\" /]
Link to comment
Share on other sites

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\" /]
Link to comment
Share on other sites

Argh, I though the /i modifier was really a lot slower than a-zA-Z? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--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]
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.