Saturday, July 9, 2011

Password Regular Expression in perl example

 Only contains alphanumeric and the following special characters: !@#$%^&*
   Contains at least 1 of the special characters in the list above
   The required special character can appear anywhere in the string
(for example: !abc, a!bc, abc!)


The regular expression you're looking for is:

/^(?=.*[\!\@\#\$\%\^\&\*])[A-Za-z0-9\!\@\#\$\%\^\&\*]+$/

which will guarantee the string contains one of your special
characters using (?=) positive lookahead.


OR

^(?=.*?[!@#$%\^&*])((?!_)[\w!@#$%\^&*])+$

It looks ensure the special character is found anywhere in the string.
Once it finds it, it matches the rest of the string as long as the
string consists of only word characters, digits, and the special
characters.

Edit: The negativelookahead prevents _ (underscore).


OR


Say you want minimum requirements of "at least one non-alphanumeric
character, at least 8 characters". Use two look-aheads:

^(?=.*[^a-zA-Z0-9])(?=.{8,}$)

Other than that - let users choose the passwords they like.

No comments: