Solved

RegEx requiring one uppercase letter and at least one number

Posted on 2009-04-05
5
536 Views
Last Modified: 2013-12-12
I have a RegEx that make sure that only alpha a-z or number 0-9 are accepted, it works but now my client wants to make sure that the password includes at least one uppercase letter some where and one number.

awawawaw or 12345678 will no longer be accepted it has to be something line awaw1awA.

It doesnt matter where the uppercase or number is I the password as long as one of each is provided.

I purchased RegexBuddy and it tell me that this is the correct syntax but it dose not work.

This is how I used it.

\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S{6,}\z

if( preg_match('\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S{6,}\z,$_POST['Password']) )

Can anyone help? Or dose anyone has a better way to check that the password has the nessesary chars the password length is 8-12  
0
Comment
Question by:rcleon
5 Comments
 
LVL 3

Expert Comment

by:cubeeq
ID: 24074081
I am afraid it is not possible with RE, because there is infinite possibilities how to count matching upper case letters, therefore it is not possible to construct finite automata on this. I suppose it is CF grammar ;-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 24074088
That requires an upper case letter, a lower case letter, a digit, and 6 or more non-space characters. so "awaw1awA" qualifies
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24074090
I suggest you use separate regexps for testing that:

$has_uppercase = preg_match('/[A-Z]/',$pass);
$has_digit = preg_match('/[1-9]/',$pass);
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24074114
Something like this:
function valid_pass($pass) {

  $invalid = preg_match('/[^A-Z0-9]/i',$pass);

  $has_uppercase = preg_match('/[A-Z]/',$pass);

  $has_digit = preg_match('/[1-9]/',$pass);

  if(!$invalid and 

     strlen($pass) >= 8 and 

     strlen($pass) <= 12 and

     $has_uppercase and

     $has_digit) return true;

  else return false;

}

Open in new window

0
 

Author Closing Comment

by:rcleon
ID: 31566882
Thank you all!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now