Re: Newbie pattern question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Phpu <phpu@xxxxx>:
> I have this function
>
>  function validate_name($name) {
>
>   if(ereg("^[a-zA-Z0-9_]{2,30}$", $name)) {
>
>    return true;
>
>   } else {
>
>    return false;
>
>   }
>
>  }
>
> If i enter a name like John for exemple everything is ok but if i enter =
> John Doe the function return false.

You need to add a space to your character class:

    if (ereg("^[a-zA-Z0-9_ ]{2,30}$", $name)) {
        return true;
    }

However, note also: pattern matching such as this is faster and more
flexible if you use the preg_* functions instead of the ereg_*
functions:

    if (preg_match('/^[a-z0-9_ ]{2,30}$/i', $name)) {
        return true;
    }

will perform faster, and gives you case insensitivity with the 'i'
switch following the regexp.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux