SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : Microsoft .NET :

Previous DiscussionNext Discussion
 Regular Expressions - Help!!

Hi all,

I've been trying to figure this out and hunted google for a solution but can't seem to find the answer. I need to create a regular expression, if possible, to check the contents of a password being entered. The password I want to check is to be at least 5 characters long, mixed case with a numeric digit.

Can anyone help...and an explanation of why it does it or how would be great. It's to be used in a validator and I know I can write a custom validator to do the job but thought a reg. exp would be simpler....

Thanks in advance.

Started By brian on Feb 26, 2007 at 2:41:18 AM

3 Response(s) | Reply

View All Replies
BachusII on Feb 26, 2007 at 5:53:38 AM

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…



View All Replies

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2000 InsideDHTML.com, LLC. All rights reserved.