Regex for password must contain at least eight cha

2018-12-31 02:50发布

I want a regular expression to check that:

A password contains at least eight characters, including at least one number and includes both lower and uppercase letters and special characters, for example #, ?, !.

It cannot be your old password or contain your username, "password", or "websitename"

And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character.

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"

How can I write it for a password must be eight characters including one uppercase letter, one special character and alphanumeric characters?

24条回答
旧时光的记忆
2楼-- · 2018-12-31 03:10

Not directly answering the question, but does it really have to be a regex?

I used to do lots of Perl, and got used to solving problems with regexes. However, when they get more complicated with all the look-aheads and other quirks, you need to write dozens of unit tests to kill all those little bugs.

Furthermore, a regex is typically a few times slower than an imperative or a functional solution.

For example, the following (not very FP) Scala function solves the original question about three times faster than the regex of the most popular answer. What it does is also so clear that you don't need a unit test at all:

def validatePassword(password: String): Boolean = {
  if (password.length < 8)
    return false

  var lower = false
  var upper = false
  var numbers = false
  var special = false

  password.foreach { c =>
    if (c.isDigit)       numbers = true
    else if (c.isLower)  lower = true
    else if (c.isUpper)  upper = true
    else                 special = true
  }

  lower && upper && numbers && special
}
查看更多
其实,你不懂
3楼-- · 2018-12-31 03:11

In Java/Android, to test a password with at least one number, one letter, one special character in following pattern:

"^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"
查看更多
余生请多指教
4楼-- · 2018-12-31 03:13

Use the following Regex to satisfy the below conditions:

Conditions: 1] Min 1 special character.
            2] Min 1 number.
            3] Min 8 characters or More

Regex: ^(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$

Can Test Online: https://regex101.com

查看更多
人间绝色
5楼-- · 2018-12-31 03:14

Try this one:

  1. Minimum six characters
  2. At least one uppercase character
  3. At least one lowercase character
  4. At least one special character

Expression:

"/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&.]{6, 20}/"

Optional Special Characters:

  1. At least one special character
  2. At least one number
  3. Special characters are optional
  4. Minimum six characters and maximum 16 characters

Expression:

"/^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$/"

If the min and max condition is not required then remove .{6, 16}

  • 6 is minimum character limit
  • 20 is maximum character limit
  • ?= means match expression
查看更多
泪湿衣
6楼-- · 2018-12-31 03:15

I had some difficulty following the most popular answer for my circumstances. For example, my validation was failing with characters such as ; or [. I was not interested in white-listing my special characters, so I instead leveraged [^\w\s] as a test - simply put - match non word characters (including numeric) and non white space characters. To summarize, here is what worked for me...

  • at least 8 characters
  • at least 1 numeric character
  • at least 1 lowercase letter
  • at least 1 uppercase letter
  • at least 1 special character

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/

JSFiddle Link - simple demo covering various cases

查看更多
永恒的永恒
7楼-- · 2018-12-31 03:15

@ClasG has already suggested:

^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)(?=\S*[^\w\s])\S{8,}$

but it does not accept _(underscore) as a special character (eg. Aa12345_).

An improved one is:

^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)(?=\S*([^\w\s]|[_]))\S{8,}$
查看更多
登录 后发表回答