Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password REGEX with min 6 chars, at least one letter and one number and may contain special characters

I need a regular expression with condition:

  • min 6 characters, max 50 characters
  • must contain 1 letter
  • must contain 1 number
  • may contain special characters like !@#$%^&*()_+

Currently I have pattern: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,50})$

However it doesn't allow special characters, does anybody have a good regex for that?

Thanks

like image 966
Budiawan Avatar asked Oct 21 '11 02:10

Budiawan


People also ask

What is the password should have a minimum of 1 special characters?

a minimum of 1 special character: ~`! @#$%^&*()-_+={}[]|\;:"<>,./? at least 1 upper case, numeric, and special character must be EMBEDDED somewhere in the middle of the password, and not just be the first or the last character of the password string.

What does ?= Mean in RegEx?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


1 Answers

Perhaps a single regex could be used, but that makes it hard to give the user feedback for which rule they aren't following. A more traditional approach like this gives you feedback that you can use in the UI to tell the user what pwd rule is not being met:

function checkPwd(str) {     if (str.length < 6) {         return("too_short");     } else if (str.length > 50) {         return("too_long");     } else if (str.search(/\d/) == -1) {         return("no_num");     } else if (str.search(/[a-zA-Z]/) == -1) {         return("no_letter");     } else if (str.search(/[^a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\_\+]/) != -1) {         return("bad_char");     }     return("ok"); } 
like image 155
jfriend00 Avatar answered Sep 21 '22 23:09

jfriend00