| BachusII on Feb 26, 2007 at 5:53:38 AM (# 1) Let's see, the string should conform to all four of the following. /^[a-zA-Z0-9]{5,}$/ A string consisting of either lowercase, uppercase or numeric digit characters, which is at least 5 characters long. The string handling functions of your programming language are likely better suited to check the minimum length. (e.g. strlen in PHP. Faster, less resource hungry, etc.) On the other hand, this regex does exlude any strings containing illegal (non-alphanumeric) characters./[a-z]/ Contains at least 1 lowercase character./[A-Z]/ Contains at least 1 uppercase character./[0-9]/ Contains at least 1 digit.
Checking OR is easy with regexes, checking AND is a more elaborate and thus more expensive operation.
For example, consider a forced mixed case string at least 2 charachters long. (Further on L (lowercase), U (uppercase) and D (numeric digit) are used to denote lowercase, uppercase or digital characters.)
/^([a-z][A-Z]|[A-Z][a-z])[a-zA-Z]*$/ "LU…" OR "UL…" followed by 0 or more of either L or U characters. Only 2 options in stead of 4 (22) because we can discard "LL…" and "UU…". The same, only 3 charachters minimum.
/^([a-z][a-z][A-Z]|[a-z][A-Z][a-z]|A-Z][a-z][a-z]|[a-z][A-Z][A-Z]|[A-Z][a-z][A-Z]|A-Z][A-Z][a-z])[a-zA-Z]*$/ "LLU…" or "LUL…" or "ULL…" or "LUU…" or "ULU…" or "UUL…", 6 options. (In stead of 8 (23) because we can discard "LLL…" and "UUU…".) See where we're going with this? 30 (25 - 2) options, just for a mixed case string 5 characters or more in length.
Back to your requirements; mixed case, a digit and at least 5 characters long. 243 (35) options, minus a couple where either one case and/or a digit is missing. That is one serious regex to write, and maintain. Yes, you could check in 3 regexes, one for absence of illegal characters and minimum length (/^[a-zA-Z0-9]{5,}$/) followed by one for case change (/([a-z][A-Z]|[A-Z][a-z])/) and one for alpha shifting (/([a-zA-Z][0-9]|[0-9][a-zA-Z])/). But if you want to do that in 1 regex…
brian on Feb 26, 2007 at 8:17:14 AM (# 2)Thanks for the reply. I think I'll just use a custom validator...it will be easier. BachusII on Feb 26, 2007 at 10:13:57 AM (# 3) This message has been edited.Severely untested. (And does pass 3 character strings, nontheless…) /^[a-zA-Z0-9]*([a-z]+[A-Z]+[0-9]+|[a-z]+[0-9]+[A-Z]+|[A-Z]+[a-z]+[0-9]+|[A-Z]+[0-9]+[a-z]+|[0-9]+[a-z]+[A-Z]+|[0-9]+[A-Z]+[a-z]+)[a-zA-Z0-9]*$/
|