need a regex for password validation

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 9592 Views - Last Post: 18 February 2010 - 09:18 AM Rate Topic: -----

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

need a regex for password validation

Posted 16 August 2008 - 05:23 AM

hi ,
i need a regex for password validation. password should contain
atleast 1 capital
atleast 1 small
atleast 1 specialchar
atleast 1 number

i'm trying with following ....
		 	$regex="/[A-Z]/"; //regular expression
		
			if (!preg_match($regex,$sPassword)) 
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[a-z]/";
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[0-9]/";
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[\W_]/"; //special characters and underscore character
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			



but i need a single line like following
$regex="/^([A-Z]){1,}+([a-z]){1,}+([0-9]{1,}+([\W_]){1,}+$/";

	if (!preg_match($regex,$sPassword)) 
	{
		print "invalid Password"."<br>"; 
		
	}


but it's not working
urgent...
any help pls...

This post has been edited by kummu4help: 16 August 2008 - 05:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need a regex for password validation

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: need a regex for password validation

Posted 16 August 2008 - 07:34 AM

Well to put it into one line then you will need those requirements to also fulfill a certain order. Remember that Regex is for "pattern" match not necessarily "content" matching. What I mean by this is that if you just want to test for their existence you will need to do the individual tests, but now if those items are being tested with a given order you are creating a pattern and that can be done in one line.

So if you want to test for your criteria in a given order, sure you can do that in one line. Otherwise you are stuck to having a few tests. :)
Was This Post Helpful? 0
  • +
  • -

#3 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 17 August 2008 - 09:30 PM

thanks Martyr2,

my password doesn't follow any particular pattern. but it has some restrictions as i mentioned already.

atleast 1 capital
atleast 1 small
atleast 1 specialchar
atleast 1 number
no spaces

just look at my code snippet and tell me is that approach is ok or can do in any better way to reduce no.of lines in code..

		 $regex="/[A-Z]/"; //regular expression
		
			if (!preg_match($regex,$sPassword))
			{
				print "Login failed.Invalid password ";
				return false;				
			}
			
			$regex="/[a-z]/";
			
			if (!preg_match($regex,$sPassword))  
			{
				print "Login failed.Invalid password ";
				return false;				
			}
			
			$regex="/[0-9]/";
			
			if (!preg_match($regex,$sPassword))  
			{
				print "Login failed.Invalid password ";
				return false;				
			}
			
			$regex="/[\W_]/"; //special characters and underscore character
			
			if (!preg_match($regex,$sPassword))  
			{
				print "Login failed.Invalid password ";
				return false;				
			}



also tell me how to find spaces.. i want my password should not contain any spaces.. if it contain spaces i have to return false;

This post has been edited by kummu4help: 17 August 2008 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: need a regex for password validation

Posted 17 August 2008 - 10:38 PM

Well with working with Regular Expressions in C# here is the pattern I use

"(?=^.{6,13}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*".

This requires at least one upper case letter, one lower case letter, one number and an optional special character. It also forces the password to be between 6 and 13 characters long (that's a good range for generating a secure password)

Try that pattern in your code and see if it helps to solve your problem


$regex = "(?=^.{6,13}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*";

if (!preg_match($regex,$sPassword))  
{
            print "Login failed.Invalid password ";
            return false;                
}


Was This Post Helpful? 1
  • +
  • -

#5 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 17 August 2008 - 11:05 PM

Hi psycho coder,

i tried with ur advice. but i'm getting following error

i'm using php...

$regex = "(?=^.{6,13}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*";

if (!preg_match($regex,$sPassword))  
{
            print "Login failed.Invalid password ";
            return false;                
}



The error is

Warning: preg_match() [function.preg-match]: Unknown modifier '(' in C:\Inetpub\wwwroot\employee\trial1.php on line 12
invalid passwordPHP Warning: preg_match() [function.preg-match]: Unknown modifier '(' in C:\Inetpub\wwwroot\employee\trial1.php on line 12 


Was This Post Helpful? 0
  • +
  • -

#6 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 17 August 2008 - 11:48 PM

hi martyr2,

i have the following code to solve my problem.
$regex = "/(?=^.{8,12}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[\W_])^.*/";

	$regex = "/(?=^.{8,12}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[\W_])^.*/";			
			
			if (!preg_match($regex,$sPassword)) 
			{ 
								print "not valid password";
				return false;				 
			}



but i didn't understand what will the last thing in regex i.e "^.*" is doing. can u explain it a bit for me pls.....

This post has been edited by kummu4help: 18 August 2008 - 03:48 AM

Was This Post Helpful? 0
  • +
  • -

#7 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 18 August 2008 - 03:56 AM

asfd

This post has been edited by kummu4help: 18 August 2008 - 03:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 18 August 2008 - 04:26 AM

^ and $ are known as 'start to end indicators'.

Regular expressions use four main methods of syntax: start and end indicators, logical operators, mathmatical operators and grouping indicators.

There are loads of great tutorials out there, including the following:

Try this

This post has been edited by pemcconnell: 18 August 2008 - 04:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 19 August 2008 - 04:01 AM

Hi ,

can some one tell me how the following regex can be improved so that it will not accept spaces..

  $regex = "/(?=^.{8,12}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[\W_])^.*/";			
			
			if (!preg_match($regex,$sPassword))
			{
								print "not valid password";
				return false;				
			}



the above will check for at least one
capital,one small , one numeric and one special character

i want to improve it so that the password will not accept any spaces..

thanks for any help
Was This Post Helpful? 0
  • +
  • -

#10 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: need a regex for password validation

Posted 19 August 2008 - 01:03 PM

I just want to say, if you're using this for a real website, it would be a pain in anyones ass to register, and they would probably forget their password a lot of times... I highly doubt ANYONE on these forums uses all of them.... my friends password is all text, mine is all numbers, another of my friends goes with both.... none of them have special characters (in fact, most passwords don't allow them)...

i'd seriously suggest you rethink what you're trying to do, it'd only aggravate users...
Was This Post Helpful? 0
  • +
  • -

#11 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 19 August 2008 - 09:08 PM

Hi Jbrace,

Thanks for ur advice but i've no other option it's our requirement so i've to do it in that way. can u pls tell me some way so that the regex will not accept spaces..
Was This Post Helpful? 0
  • +
  • -

#12 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 20 August 2008 - 01:06 AM

I haven't used regular expressions in a long time but as far as I can remember, in php the s character is used to distinguish whitespace, so to test for no whitespaces you would simply add the following to your regex:

(\s{0})

Which means whitespaces must be represented 0 times.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#13 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 20 August 2008 - 04:10 AM

Hi ,

can some one tell me how the following regex can be improved so that it will not accept spaces..


  $regex = "/(?=^.{8,12}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[\W_])^.*/";			
			
			if (!preg_match($regex,$sPassword))
			{
								print "not valid password";
				return false;				
			}




i want to improve it so that the password will not accept any spaces..

thanks for any help
Was This Post Helpful? 0
  • +
  • -

#14 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: need a regex for password validation

Posted 20 August 2008 - 05:06 AM

weird - i thought i added a post here earlier.

Been a long time since i've used regular expressions so this might be a bit rusty, but here goes:

Basically you want to use the \s e.g. [\s]{0} which means [require whitespace]{zero times}

Test it though - hopefully i haven't gave you a red herring
Was This Post Helpful? 0
  • +
  • -

#15 Guest_vikal*


Reputation:

Re: need a regex for password validation

Posted 06 February 2010 - 02:55 AM

View Postkummu4help, on 16 August 2008 - 04:23 AM, said:

hi ,
i need a regex for password validation. password should contain
atleast 1 capital
atleast 1 small
atleast 1 specialchar
atleast 1 number

i'm trying with following ....
		 	$regex="/[A-Z]/"; //regular expression
		
			if (!preg_match($regex,$sPassword)) 
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[a-z]/";
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[0-9]/";
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			
			$regex="/[\W_]/"; //special characters and underscore character
			
			if (!preg_match($regex,$sPassword))  
			{ 
				$this->printDebug(cSYS_LOG_LEVEL_ERROR, "Login failed.Invalid password ");
				return false;				 
			}
			



but i need a single line like following
$regex="/^([A-Z]){1,}+([a-z]){1,}+([0-9]{1,}+([\W_]){1,}+$/";

	if (!preg_match($regex,$sPassword)) 
	{
		print "invalid Password"."<br>"; 
		
	}


but it's not working
urgent...
any help pls...


Use This:
if (preg_match("/^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/", $str)) {
return true;
}
else {
return false;
}
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2