Jump to content

Archived

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

JackJack

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

Recommended Posts

How do i get it to say"invalid username" if it contains symbol which isnt "a-Z 0-9 -_"?

thank you

Share this post


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

Share this post


Link to post
Share on other sites
Only you also want to allow 0-9, so you have to add 0-9 between the square backets.

Share this post


Link to post
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\" /]

Share this post


Link to post
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 $.

Share this post


Link to post
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...

Share this post


Link to post
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\" /]

Share this post


Link to post
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\" /]

Share this post


Link to post
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\" /]

Share this post


Link to post
Share on other sites
Thank you for all your help but how can i make it doesnt have one of those letters in it will come up with an error????

Ty

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.