Jump to content

add apostrophe and dash in preg_match


dadamssg

Recommended Posts

if(!preg_match("/^[-'a-z0-9]{8,12}$/i",$_POST['username']))

 

You may need to stripslashes $_POST['username'] before preg_matching though.

 

Also, that pattern does not prevent stuff like this from matching:

a-----bc

a''''''''bc

a-b-c-d-e

------abc--

etc..

Well, you could use substr_count() to count how many times a char appears in a text.

 

ex:

$user = $_POST['username']; 
if (!preg_match("/^[-'a-z0-9]{8,12}$/i",$user) || substr_count($user, "'") > 1 || substr_count($user, "-") > 1)
{
echo "Invalid";
}
else
{
echo "Valid";
}

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.