[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Regex for alphanumeric only

Posted on 2010-03-30
4
Medium Priority
?
191 Views
Last Modified: 2012-06-27

* must contain both upper and lowercase characters
* must contain at least one number
* must only contain letters and numbers (no other special characters)
* min length 7, max length 12

The following is close:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*[@#$%^&+=]).{7,12}$

but there is a danger i could miss characters in the exclude list.  Is there anyway to specify just alphanumeric only?

Thanks
0
Comment
Question by:Sandy209
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 29096063
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])([0-9a-zA-Z]){7,12}$
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 29096144
I think this is better.

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])([0-9a-zA-Z]{7,12})$
0
 

Author Closing Comment

by:Sandy209
ID: 31708916
Excellent!  Thanks
0
 
LVL 3

Expert Comment

by:Mike_Mozhaev
ID: 29097752
I would not use regex in this case. Finite state machine would be much simpler.

Something like:

bool upperCaseFound = false;
bool lowerCaseFound = false;
bool digitFound = false;

foreach (char c in s)
{
    switch (char.GetUnicodeCategory(c))
    {
        case UnicodeCategory.UppercaseLetter:
            upperCaseFound = true;
            break;

        case UnicodeCategory.LowercaseLetter:
            lowerCaseFound = true;
            break;

        case UnicodeCategory.DecimalDigitNumber:
            digitFound = true;
            break;
    }
}

bool validPassword =
    lowerCaseFound &&
    upperCaseFound &&
    digitFound &&
    s.Length >= 7 &&
    s.Length <= 12;
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question