密码的正则表达式必须包含至少八个字符,至少一个数字以及大小写字母和特殊字符

我想要一个正则表达式来检查:

密码包含至少八个字符,包括至少一个数,并且包括下部和大写字母和特殊字符都,例如#?!

它不能是您的旧密码或包含用户名"password",或"websitename"

这是我的验证表达式,它包含八个字符,包括一个大写字母,一个小写字母以及一个数字或特殊字符。

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

如何编写密码,密码必须是八个字符,包括一个大写字母,一个特殊字符和字母数字字符

Harry小胖2020/03/17 11:48:55
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$

Link check online https://regex101.com/r/mqGurh/1

逆天Stafan2020/03/17 11:48:54

Pattern to match at least 1 upper case character, 1 digit and any special characters and the length between 8 to 63.

"^(?=.[a-z])(?=.[A-Z])(?=.*\d)[a-zA-Z\d\W]{8,63}$"

This pattern was used for JAVA programming.

凯Harry2020/03/17 11:48:54

Try this:

^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])[a-zA-Z0-9@#$%^&+=]*$

This regular expression works for me perfectly.

function myFunction() {
    var str = "c1TTTTaTTT@";
    var patt = new RegExp("^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])[a-zA-Z0-9@#$%^&+=]*$");
    var res = patt.test(str);
    console.log("Is regular matches:", res);
}
梅樱2020/03/17 11:48:54

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,}$"
猿Sam2020/03/17 11:48:54

Use this,

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%?=*&]).{8,20})

It will validate for at least one lowercase, one upper case, one number and the special charecters of (!,@,#,$,%,?,=,*,&).

Minimum length is 8 and maximum length is 20

古一Near2020/03/17 11:48:54

You can use the below regular expression pattern to check the password whether it matches your expectations or not.

((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*()]).{8,20})
Jim飞云2020/03/17 11:48:54

Just we can do this by using HTML5.

Use below code in pattern attribute,

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

It will work perfectly.

前端阳光2020/03/17 11:48:54

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

阿飞米亚2020/03/17 11:48:54

@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,}$
神乐猪猪2020/03/17 11:48:54
(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+-]).{6}
泡芙Gil2020/03/17 11:48:54

According to your need this pattern should work just fine. Try this,

^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}

Just create a string variable, assign the pattern, and create a boolean method which returns true if the pattern is correct, else false.

Sample:

String pattern = "^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}";
String password_string = "Type the password here"

private boolean isValidPassword(String password_string) {
    return password_string.matches(Constants.passwordPattern);
}
Near小小2020/03/17 11:48:53

What about considering the following regex solution:

^(?=.*[\w])(?=.*[\W])[\w\W]{8,}$

Which validates the following:

  1. At least one lowercase
  2. At least one uppercase
  3. At least one digit
  4. At least one special character
  5. At least it should have 8 characters long.

Check it out working at the following link https://regex101.com/r/qPmC06/4/

Itachi达蒙2020/03/17 11:48:53
Use the following Regex to satisfy the below conditions:

Conditions: 1] Min 1 uppercase letter.
            2] Min 1 lowercase letter.
            3] Min 1 special character.
            4] Min 1 number.
            5] Min 8 characters.
            6] Max 30 characters.

Regex: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,30}$/    
小哥Gil2020/03/17 11:48:53

我会回答彼得·莫滕森,但我的声誉不高。

他的表情非常适合每个指定的最低要求。他不需要特殊字符的表达式的问题在于它们也不允许特殊字符,因此它们也强制执行最高要求,我不认为OP会要求这样做。通常,您希望允许用户设置他们想要的密码。为什么限制强密码?

因此,他的“最少八个字符,至少一个字母和一个数字”的表达是:

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$

达到最低要求,但其余字符只能是字母和数字。要允许(但不要求)特殊字符,应使用类似以下内容的字符:

^(?=.*[A-Za-z])(?=.*\d).{8,}$ 允许任何字符

要么

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d$@$!%*#?&]{8,}$ 允许特定的特殊字符

同样,“至少八个字符,至少一个大写字母,一个小写字母和一个数字:”

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

满足最低要求,但允许使用字母和数字。采用:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ 允许任何字符

要么

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d$@$!%*?&]{8,} 允许使用特定的特殊字符。

Gil蛋蛋Tony2020/03/17 11:48:53

试试这个:

  1. 至少六个字符
  2. 至少一个大写字符
  3. 至少一个小写字符
  4. 至少一个特殊字符

表达:

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

可选特殊字符:

  1. 至少一个特殊字符
  2. 至少一个号码
  3. 特殊字符是可选的
  4. 最少6个字符,最多16个字符

表达:

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

如果不需要最小和最大条件,则删除 .{6, 16}

  • 6是最小字符数限制
  • 最大字符数限制为20
  • ?=表示匹配表达式
老丝梅神乐2020/03/17 11:48:53

导入JavaScript文件jquery.validate.min.js

您可以使用此方法:

$.validator.addMethod("pwcheck", function (value) {
    return /[\@\#\$\%\^\&\*\(\)\_\+\!]/.test(value) && /[a-z]/.test(value) && /[0-9]/.test(value) && /[A-Z]/.test(value)
});
  1. 至少一个大写英文字母
  2. 至少一个小写英文字母
  3. 至少一位数字
  4. 至少一个特殊字符
番长蛋蛋Davaid2020/03/17 11:48:53

对于我的情况,我在遵循最受欢迎的答案时遇到了一些困难。例如,我的验证因诸如;或的字符而失败[我对将特殊字符列入白名单不感兴趣,因此我将其[^\w\s]用作测试-简单地说-匹配非文字字符(包括数字)和非空白字符。总而言之,这是对我有用的...

  • 至少8字符
  • 至少1数字字符
  • 至少1小写字母
  • 至少1大写字母
  • 至少有1特殊字符

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

JSFiddle Link-涵盖各种情况的简单演示

泡芙小胖2020/03/17 11:48:53

@anubhava的答案仅作一个小改进:由于特殊字符仅限于键盘上的字符,因此可将其用于任何特殊字符:

^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,}$

此正则表达式将执行以下规则:

  • 至少一个大写英文字母
  • 至少一个小写英文字母
  • 至少一位数字
  • 至少一个特殊字符
  • 长度最少八位
米亚伽罗村村2020/03/17 11:48:53

您可以将此正则表达式与多个先行断言(条件)一起使用

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$

此正则表达式将执行以下规则:

  • 至少一个大写英文字母(?=.*?[A-Z])
  • 至少一个小写英文字母, (?=.*?[a-z])
  • 至少一位数字, (?=.*?[0-9])
  • 至少一个特殊字符, (?=.*?[#?!@$%^&*-])
  • 最小长度为八.{8,}(带锚)
泡芙小卤蛋Tom2020/03/17 11:48:53

至少八个字符,至少一个字母和一个数字:

"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"

至少八个字符,至少一个字母,一个数字和一个特殊字符:

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

至少八个字符,至少一个大写字母,一个小写字母和一个数字:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"

至少八个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:

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

最少8个字符,最多10个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:

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