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 ~).
help writing an expression
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: help writing an expression
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 ~
}