...

View Full Version : PHP Regular Expressions



harkly
07-12-2012, 10:06 PM
For my preg_match I am allowing a-z, A_Z, 0-9 and the following - _ .

How do I make it so that there cannot be anymore then 1 of the underscore, dash and period in a row??

so you can do this

jane.doe.whatever

but never this

Jane..doe


if (preg_match('/[^a-z0-9-_\.]/i', $userID))

Keleth
07-12-2012, 10:32 PM
We'll wanna double check this but...


if (preg_match('/\.{2,}|-{2,}|_{2,}/', $userID)) echo 'Invalid!'

harkly
07-16-2012, 10:13 PM
No that doesn't work. Allows you to put as many as you want.

gvre
07-16-2012, 11:00 PM
You can do it using the following two patterns or one more sophisticated.

$userID = "Jane..doe";
if (preg_match('#^[a-z0-9_.-]+$#i', $userID) && !preg_match('#([._-])\1#', $userID))
echo "valid";
else
echo "invalid";

harkly
07-17-2012, 09:25 PM
Thanks! That did it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum