A thread on House of Fusion’s CF-Talk about Regular Expressions consumed me for a few hours yesterday. Someone asked an innocent question about creating a pattern to ensure that a string of characters between 6 and 12 characters in length would contain at least one number. He didn’t specify, but I assumed this was for a password field.
I ended up with at least 5 versions of my final solution that I was sure were right – until I tested them. Sometimes I didn’t even test them correctly and posted an invalid pattern to the list. I finally went to one of my favorite sites, Regular-Expressions.info and had to re-read the lookahead section a few times. Then after a few false starts I was finally able to develop something that works.
This will ensure that a password has only alphanumeric characters and no whitespace.
^(?=.*[A-Za-z])(?=.*[0-9])(?!.*[^A-Za-z0-9])(?!.*\s).{6,12}$
This pattern will allow any non-whitespace character passwords
^(?=.*[A-Za-z])(?=.*[0-9])(?!.*\s).{6,12}$
If you want to get really crazy, you can start enforcing strong passwords where at least one character must be lower case alpha and one must be upper case.
^(?=.*[A-Za-z])(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?!.*[^A-Za-z0-9])(?!.*\s).{6,12}$
9 Responses for "Password Regular Expression Fun"
While I know that the point here was to exercise regular expressions, surely a couple simple tests applied in sequence would be a better solution:
len(string) GTE 6 AND len(string LTE 12 AND REFindNoCase(“[^a-z0-9]“, string) EQ 0 AND REFind(“[0-9]“, string) GT 0
Not only is that readable, but changing something (like enforcing both upper and lowercase letters) is far simpler.
Well sure, but that takes all the fun out of it! Who doesn’t want a monstrous regex pattern in their code that is hard to comprehend and difficult to change?
Sheesh, Barney, you don’t have to get all logical on me here, and I just thought of the one and only time that using the regex pattern is easier… if you store your patterns in a database. No need to ask why one would do that, I have no idea, but IT COULD HAPPEN.
Anyway, you’re absolutely right and I said as much when I posted the solution in the CF-Talk thread that using multiple conditions is far easier.
Hi
I have tried following regular expression but its not working properly.Please look into that & give me a correct regular expression.
Requirement is :
number of char is between (8,25),
at least one numeric,
at least one upper case,
at least one lower case.
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,25}$
I need a regular expression that tracks that the passowrd is of minimum 8 characters and sould contain atleat one alphabet, one digit and one special character
i require a regex pattern to check the password with following conditions:
->min 5 characters and a max of 10
->only alphanumeric
->no whitespaces or special characters
->atleast one number in the password
i require a regex pattern to check the password with following conditions:
->min 5 characters and a max of 10
->only alphanumeric
->no whitespaces or special characters
->atleast one number in the password
I require a regex pattern to check the password with following conditions:
Min 6 characters and a Max of 15
1.One Uper character
2.One lower character
3.One speceial character
4.One number(0-9)
passwrod should not contain any whitespaces
At least 3 of 4 above mentioned conditions should match to be valid a password
Thanks in advance.
Barney,
Solving the problem with logic is great, but sometimes you have to solve the problem with data so you can store said logic somewhere, and a regular expression provides you with a great way to do.
I am having a hard time with a pass word for a twic card it keeps saying pass word does no match
Leave a reply