Jump to content


Photo

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


  • Please log in to reply
12 replies to this topic

#1 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 14 May 2006 - 03:03 PM

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

thank you

#2 wildteen88

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

Posted 14 May 2006 - 04:24 PM

use preg_match

<?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
}

?>

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

Also sorted out a few problems too


#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 14 May 2006 - 04:32 PM

Only you also want to allow 0-9, so you have to add 0-9 between the square backets.

#4 wildteen88

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

Posted 14 May 2006 - 04:35 PM

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

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 14 May 2006 - 04:44 PM

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 $.

#6 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 14 May 2006 - 04:57 PM

[!--quoteo(post=373773:date=May 14 2006, 11:44 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 14 2006, 11:44 AM) View Post[/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...

#7 wildteen88

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

Posted 14 May 2006 - 05:01 PM

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

#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 14 May 2006 - 05:19 PM

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

#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 14 May 2006 - 05:30 PM

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

#10 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 14 May 2006 - 06:04 PM

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

#11 haydndup

haydndup
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationJohannesburg, South Africa

Posted 14 May 2006 - 06:10 PM

What do you mean? the code that wildteen88 posted includes if statements to handle the response of the preg_match.

<?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
}

?>

Just change these parts:

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


[!--coloro:#660000--][span style="color:#660000"][!--/coloro--]The Gene Pool Could Use A Little Chlorine[!--colorc--][/span][!--/colorc--]

#12 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 14 May 2006 - 06:16 PM

I wish it were that simple.

Heres the chunk of php im using.

}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;
}

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

:S

#13 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 14 May 2006 - 06:27 PM

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

Heres the chunk of php im using.

}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;
}

$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]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users