9 Replies - 9215 Views - Last Post: 01 December 2008 - 12:53 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

regex for password validation

Post icon  Posted 28 November 2008 - 09:27 PM

Hi
i want to have a regular expression for password validation
i am trying the following
	$val="good_123*#";
	
	if(preg_match('/^[a-z0-9\w_*#]*$/i', $val))
	{
		print "alpha numeric";
	}
	else
	{
		print "not alphanumeric";
	}



what i want to do is, if user enters a password, then i will check whether it is alpha numeric or not.
alpha numeric for me is
it should be a combination of alphabets,digits and specialcharacters except space
i don't want user to put space in his password.But he can enter any specialcharacter.

can anyone give me a better regex for this context.....

previously i got help from martyr2 for similar type of question
hope again i will get from him

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: regex for password validation

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: regex for password validation

Posted 28 November 2008 - 09:49 PM

If you want just alphanumeric, use
if(preg_match('/^[a-z0-9]*$/i', $val))

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: regex for password validation

Posted 28 November 2008 - 10:14 PM

Hi jackofalltrades
i need regex for alphanumeric and also for special characters

any how thanks for quick response
still waiting for other geeks voice
Was This Post Helpful? 0
  • +
  • -

#4 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: regex for password validation

Posted 28 November 2008 - 10:22 PM

Hi
i think if i can make the following to work most probably i am done.
	if(preg_match('/^[a-zA-Z][0-9][:punct:]*$/i', $val))
	{
		print "alpha numeric";
	}
	else
	{
		print "not alphanumeric";
	}


but unfortunately i am getting the following error
Warning: preg_match() [function.preg-match]: Compilation failed: POSIX named classes are supported only within a class at offset 14 in E:\playground\NCVSR\NCVSR_PROJECTS\NCVSM_ADOBILE\dev\version_1_0\web\temp\test.php on line 6
not alphanumericPHP Warning: preg_match() [function.preg-match]: Compilation failed: POSIX named classes are supported only within a class at offset 14 in E:\playground\NCVSR\NCVSR_PROJECTS\NCVSM_ADOBILE\dev\version_1_0\web\temp\test.php on line 6



can any one fix it
Was This Post Helpful? 0
  • +
  • -

#5 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: regex for password validation

Posted 28 November 2008 - 11:32 PM

Now i have the following regex
i think it's working fine
	if(preg_match('/^[\w]+[\W]+$/i', $val))
	{
		print "alpha numeric";
	}
	else
	{
		print "not alphanumeric";
	}


but i want to limit the length of string between 6 and 8

also can anyone point out the loopholes this regex may have. i must enforce user to enter atleast one alphanumeric(numbers or alphabets) and atleast one special character

also i want to limit user from entering spaces.
i.e the password should not contain any spaces(can it be enforced or not. i welcome any suggestion )
so how can i do that

This post has been edited by kummu4help: 28 November 2008 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,858
  • Joined: 18-April 07

Re: regex for password validation

Posted 28 November 2008 - 11:35 PM

There are two ways to do this. In the positive or in the negative. The positive way is to list all the characters simply in the square brackets....

// Here we have letters, numbers some special characters
// But notice we did not add spaces \s to the list
preg_match('/^[a-zA-Z0-9$!@#%&\.\?]*$/i', $val)



The other way is in the negative where we say we want everything BUT these values...


// Here we say everything, but spaces (notice the caret inside the brackets
// this is saying to not what is in the list)
preg_match('/^[^\s]*$/i', $val)



So you can choose one method or the other and see which makes more sense to you.

Edit: And as for length, you can use the min, max parameters inside of curly braces...

// Match at least 6 and no more than 8 digits in a row
preg_match('/\d{6,8}/i', $val)





Hope this gets you up and running with something you can work with.

"At DIC we be password validating, but not with spaces, coding ninjas... yeahspacesreallysuckinpasswords" :snap:

This post has been edited by Martyr2: 28 November 2008 - 11:37 PM

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: regex for password validation

Posted 29 November 2008 - 12:17 AM

Thanks martyr2.

but i am not completly done yet.

i need to modify the following regex. so that it can restrict spaces in the string and also it will restrict the length condition also. i mean this regex should be able to check whether the string is between 6 to 8 chars or not.
	if(preg_match('/^[\w]+[\W]+$/i', $val))
	{
		print "alpha numeric";
	}
	else
	{
		print "not alphanumeric";
	}


i need to add those conditions to the above regex.how can i do that

i am trying the following but its not working
[code]
preg_match('/^[\w]+[\W]+[^\s]{6,8)$/i', $val)
[\code]
can someone correct the above regex

thanks
Was This Post Helpful? 0
  • +
  • -

#8 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: regex for password validation

Posted 30 November 2008 - 09:22 PM

ok

i am running out of time. so i want to compromise a bit myself.

can anyone give me a regex to validate the password with following conditions

Quote

1. password should have atleast one alphabet and atleast one digit.
2.password should be atleast 6 characters and maximum of 8.
3. also password can allow specialcharacters but it is not mandatory.
without spl.characters also password is valid but it should have atleast one alphabet and minimum 1 digit.


hope i am clear.
i tried with ctype_alnum() function in php but it is accepting if all characters or either alphabet or digit. but i want to enforce atleast one alphabet and one digit should be in password

can any one give me a regex for this pls.....

This post has been edited by kummu4help: 30 November 2008 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,858
  • Joined: 18-April 07

Re: regex for password validation

Posted 30 November 2008 - 09:35 PM

Re-read my last response, I had made an edit regarding the minimum and maximum of the password length. I guess you didn't catch it. :)
Was This Post Helpful? 0
  • +
  • -

#10 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: regex for password validation

Posted 01 December 2008 - 12:53 AM

Hi ,
at last i was succeded in writing a regex that suits my requirements.
	if(false===(preg_match("/^(?=.*\d)(?=.*[a-z]).{6,8}$/", $sInput) > 0))
	{
		return false;
	}



it was working fine
now can anyone tell me how can i use the above expression in javascript.

thanks
Was This Post Helpful? 1

Page 1 of 1