Page 1 of 1

help writing an expression

Posted: Tue May 06, 2008 9:22 am
by gammaman
I need an expression that does the following

A length of six characters with the first character being a letter. It must contain at least on upper case letter, a number and a symbol (% or # or ~).

Re: help writing an expression

Posted: Tue May 06, 2008 10:13 am
by prometheuzz
gammaman wrote:I need an expression that does the following

A length of six characters with the first character being a letter. It must contain at least on upper case letter, a number and a symbol (% or # or ~).

Trying to do it all in one match would be madness, break it up in several matches:

Code: Select all

function is_valid($text) {
    return preg_match('/^[a-zA-Z][a-zA-Z\d%#~]{5}$/', $text) && // starts with a letter, followed by 5  
                                                                // characters in the set [a-zA-Z\d%#~]
           preg_match('/\d/', $text) &&                         // contains a number
           preg_match('/[a-z]/', $text) &&                      // contains a lower case letter
           preg_match('/[A-Z]/', $text) &&                      // contains an upper case letter
           preg_match('/[%#~]/', $text);                        // contains one of %, # or ~
}