Reply to Re: [PHP] validating input

Your name:

Reply:


Posted by trlists on 10/10/49 11:13

On 12 Apr 2005 blackwater dev wrote:

> $good = "joh_'";
>
> // Let's check the good e-mail
> if (preg_match("/[a-z0-9]/", $good)) {
> echo "Good";
> } else {
> echo "Bad";
> }
>
> This returns Good, why?

That regex matches any string which contains at least one (lowercase)
letter or one number somewhere within it. To match for only those
elements and no others you probably want:

if (preg_match("/^[a-z0-9]*$/", $good)) {

which will match a string which contains nothing but a-z and 0-9, and
will also match the empty string. See
http://www.php.net/manual/en/reference.pcre.pattern.syntax.php and look
at the meanings of ^, $, *, and +.

Other variations for the regex:

/^[a-z0-9]+$/ as above but will not match empty string
/^[a-z0-9]*$/i as above but matches upper case as well
/^[a-zA-Z0-9]*$/ like previous line -- as above but matches upper
case as well

As Chris S. pointed out, a regex is not needed for this task, so the
above is just academic.

--
Tom

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация