简体   繁体   English

Javascript中密码验证的正则表达式

[英]Regex for Password validation in Javascript

Regex Password complexity requires that any three of the following four characteristics must be applied when creating or changing a password.正则表达式密码复杂性要求在创建或更改密码时必须应用以下四个特征中的any three

  • Alpha characters - at least 1 upper case alpha character字母字符 - 至少 1 个大写字母字符
  • Alpha characters - at least 1 lower case alpha character字母字符 - 至少 1 个小写字母字符
  • Numeric characters - at least 1 numeric character数字字符 - 至少 1 个数字字符
  • Special characters - at least 1 special character特殊字符 - 至少 1 个特殊字符

I am trying with the following code, but its not working for special characters我正在尝试使用以下代码,但它不适用于special characters

(?=^.{6,}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*

I want my regex to be validated against the following 4 cases我希望针对以下 4 种情况验证我的正则表达式

Match cases火柴盒

  • P@ssword P@ssword
  • Password1密码1
  • p@ssword1 p@ssword1
  • p@12345电话@12345

I think a single regex will be messy in this case. 我认为在这种情况下单个正则表达式会很混乱。 You can easily do something like 你可以轻松做点什么

var count = 0;

count += /[a-z]/.test(password) ? 1 : 0;
count += /[A-Z]/.test(password) ? 1 : 0;
count += /\d/.test(password) ? 1 : 0;
count += /[@]/.test(password) ? 1 : 0;

if(count > 2) {
    alert('valid')
}

I think that a regex you can use is: 我认为你可以使用的正则表达式是:

(?=^.{6,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9]).*

I'm not sure why you have so many or operators in your regex but this one matches if: 我不确定你为什么在你的正则表达式中有这么多或运算符,但这个匹配如果:

  • (?=^.{6,}$) - String is > 5 chars (?=^.{6,}$) - 字符串> 5个字符
  • (?=.*[0-9]) - Contains a digit (?=.*[0-9]) - 包含一个数字
  • (?=.*[AZ]) - Contains an uppercase letter (?=.*[AZ]) - 包含一个大写字母
  • (?=.*[az]) - Contains a lowercase letter (?=.*[az]) - 包含一个小写字母
  • (?=.*[^A-Za-z0-9]) - A character not being alphanumeric. (?=.*[^A-Za-z0-9]) - 不是字母数字的字符。

正则表达图像

Use this Regex : 使用此正则表达式:

(?=^.{6,10}$)(?=. \\d)(?=. [az])(?=. [AZ])(?=. [!@#$%^&*()_+}{":;'?/>.<,])(?!. \\s). $** (?= ^。{6,10} $)(?=。 \\ d)(?=。 [az])(?=。 [AZ])(?=。 [!@#$%^&*() _ +} {“:;'?/>。<,])(?!。 \\ s)。 $ **

I think you would need this for all special characters too : [updated to reject space] 我想你也需要这个所有特殊字符:[更新拒绝空间]

    $(document).ready(function(){
    $('input').change(function(){
    var count = 0;
    var pass = $(this).val();
        count += /[a-z]/.test(pass) ? 1 : 0;
        count += /[A-Z]/.test(pass) ? 1 : 0;
        count += /\d/.test(pass) ? 1 : 0;
        count += /[^\w\d\s]/.test(pass) ? 1 : 0;
        (count>2 & !/[\s]+/.test(pass)) ? $(this).css('background-color','lime'):$(this).css('background-color','orange');
    });

});

and the fiddle : jsFiddle 和小提琴: jsFiddle

var pattern = new RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/);

    if(pattern.test(value)){
         return true;
    } else {
         return false;
    }

Its working fine with special character also. 它的工作正常也具有特殊性。

Non English UTF-8非英语 UTF-8<\/h2>

None of the solutions given allow international letters, ie éÉöÖæÆáÁ, but mainly focus on the english ASCII<\/code> alphabet.给出的解决方案都不允许使用国际字母,即 éÉöÖæÆáÁ,但主要关注英文ASCII<\/code>字母表。

The following regEx uses unicode, UTF-8, to recognise upper and lower case and thus, allow international characters:以下正则表达式使用 unicode UTF-8 来识别大小写,因此允许使用国际字符:

 \/\/ Match uppercase, lowercase, digit or #$!%*?& and make sure the length is 6 to 36 in length const pwdFilter = \/^(?=.*\\p{Ll})(?=.*\\p{Lu})(?=.*[\\d|@#$!%*?&])[\\p{L}\\d@#$!%*?&]{6,36}$\/gu if (!pwdFilter.test(pwd)) { \/\/ Show error that password has to be adjusted to match criteria }<\/code><\/pre>

The regEx:正则表达式:

\/^(?=.*\\p{Ll})(?=.*\\p{Lu})(?=.*[\\d|@#$!%*?&])[\\p{L}\\d@#$!%*?&]{6,36}$\/gmu<\/code>

checks if an uppercase, lowercase, digit or @#$!%*?& are used in the password.检查密码中是否使用了大写、小写、数字或@#$!%*?&。 It also limits the length to be 6 minimum and maximum 36 (note that emojis, 😀🇺🇸🇪🇸🧑‍💻, count as more than one character in the length).它还将长度限制为最小 6 个和最大 36 个(请注意,表情符号😀🇺🇸🇪🇸🧑‍💻在长度中算作多个字符)。 The u<\/code> in the end, is for using UTF-8.最后的u<\/code>用于使用 UTF-8。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM