本文翻译自:Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
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,
And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character. 这是我的验证表达式,它包含八个字符,包括一个大写字母,一个小写字母和一个数字或特殊字符。
1 | (?=^.{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 ? 如何编写密码,密码必须是八个字符,包括一个大写字母,一个特殊字符和字母数字字符 ?
#1楼
参考:https://stackoom.com/question/1KGC6/密码的正则表达式必须包含至少八个字符-至少一个数字以及大小写字母和特殊字符
#2楼
You may use this regex with multiple lookahead assertions (conditions) : 您可以将此正则表达式与多个先行断言(条件)一起使用 :
1 | ^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$ |
This regex will enforce these rules: 此正则表达式将执行以下规则:
- At least one upper case English letter ,
(?=.*?[AZ]) 至少一个大写英文字母 ,(?=.*?[AZ]) - At least one lower case English letter,
(?=.*?[az]) 至少一个小写英文字母(?=.*?[az]) - At least one digit,
(?=.*?[0-9]) 至少一位数字(?=.*?[0-9]) - At least one special character,
(?=.*?[#?!@$%^&*-]) 至少一个特殊字符(?=.*?[#?!@$%^&*-]) - Minimum eight in length
.{8,} (with the anchors) 长度最少为八位.{8,} (带锚点)
#3楼
Just a small improvement for @anubhava's answer: Since special character are limited to the ones in the keyboard, use this for any special character: @anubhava的答案仅作一个小改进:由于特殊字符仅限于键盘上的字符,因此可将其用于任何特殊字符:
This regex will enforce these rules: 此正则表达式将执行以下规则:
- At least one upper case English letter 至少一个大写英文字母
- At least one lower case English letter 至少一个小写英文字母
- At least one digit 至少一位数字
- At least one special character 至少一个特殊字符
- Minimum eight in length 长度最少八位
#4楼
Minimum eight characters, at least one letter and one number: 至少八个字符,至少一个字母和一个数字:
1 | "^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$" |
Minimum eight characters, at least one letter, one number and one special character: 至少八个字符,至少一个字母,一个数字和一个特殊字符:
1 | "^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$" |
Minimum eight characters, at least one uppercase letter, one lowercase letter and one number: 至少八个字符,至少一个大写字母,一个小写字母和一个数字:
1 | "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$" |
Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character: 至少八个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:
1 | "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$" |
Minimum eight and maximum 10 characters, at least one uppercase letter, one lowercase letter, one number and one special character: 最少8个字符,最多10个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:
1 | "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,10}$" |
#5楼
Import the JavaScript file
You can use this method: 您可以使用以下方法:
1 2 3 | $.validator.addMethod("pwcheck", function (value) { return /[\@\#\$\%\^\&\*\(\)\_\+\!]/.test(value) && /[a-z]/.test(value) && /[0-9]/.test(value) && /[A-Z]/.test(value) }); |
- At least one upper case English letter 至少一个大写英文字母
- At least one lower case English letter 至少一个小写英文字母
- At least one digit 至少一位数字
- At least one special character 至少一个特殊字符
#6楼
I had some difficulty following the most popular answer for my circumstances. 对于我的情况,我在遵循最受欢迎的答案时遇到了一些困难。 For example, my validation was failing with characters such as
- at least
8 characters 至少8 字符 - at least
1 numeric character 至少1 数字字符 - at least
1 lowercase letter 至少1 个小写字母 - at least
1 uppercase letter 至少1 大写字母 - at least
1 special character 至少1 特殊字符
1 | /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/ |
JSFiddle Link - simple demo covering various cases JSFiddle Link-涵盖各种情况的简单演示