Javascript 具有特殊字符的Javascript正则表达式密码验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12090077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:04:57  来源:igfitidea点击:

Javascript regular expression password validation having special characters

javascriptregexpasswords

提问by Srikanth Sridhar

I am trying to validate the password using regular expression. The password is getting updated if we have all the characters as alphabets. Where am i going wrong ? is the regular expression right ?

我正在尝试使用正则表达式验证密码。如果我们将所有字符都作为字母,密码就会更新。我哪里错了?正则表达式对吗?

function validatePassword() {
    var newPassword = document.getElementById('changePasswordForm').newPassword.value;
    var minNumberofChars = 6;
    var maxNumberofChars = 16;
    var regularExpression  = /^[a-zA-Z0-9!@#$%^&*]{6,16}$/;
    alert(newPassword); 
    if(newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars){
        return false;
    }
    if(!regularExpression.test(newPassword)) {
        alert("password should contain atleast one number and one special character");
        return false;
    }
}

回答by Jo?o Silva

Use positive lookaheadassertions:

使用积极的前瞻断言:

var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;

Without it, your current regex only matches that you have 6 to 16 valid characters, it doesn'tvalidate that it has at leasta number, and at leasta special character. That's what the lookahead above is for.

没有它,您当前的正则表达式只匹配您有 6 到 16 个有效字符,它不会验证它至少有一个数字,至少有一个特殊字符。这就是上面的前瞻。

  • (?=.*[0-9])- Assert a string has at least one number;
  • (?=.*[!@#$%^&*])- Assert a string has at least one special character.
  • (?=.*[0-9])- 断言一个字符串至少有一个数字;
  • (?=.*[!@#$%^&*])- 断言一个字符串至少有一个特殊字符。

回答by atul-tagra

function validatePassword() {
    var p = document.getElementById('newPassword').value,
        errors = [];
    if (p.length < 8) {
        errors.push("Your password must be at least 8 characters"); 
    }
    if (p.search(/[a-z]/i) < 0) {
        errors.push("Your password must contain at least one letter.");
    }
    if (p.search(/[0-9]/) < 0) {
        errors.push("Your password must contain at least one digit."); 
    }
    if (errors.length > 0) {
        alert(errors.join("\n"));
        return false;
    }
    return true;
}

There is a certain issue in below answer as it is not checking whole string due to absence of [ ] while checking the characters and numerals, this is correct version

以下答案存在某个问题,因为在检查字符和数字时由于缺少 [ ] 而没有检查整个字符串,这是正确的版本

回答by Ujjaval

you can make your own regular expression for javascript validation

您可以为javascript验证制作自己的正则表达式

    /^            : Start
    (?=.{8,})        : Length
    (?=.*[a-zA-Z])   : Letters
    (?=.*\d)         : Digits
    (?=.*[!#$%&? "]) : Special characters
    $/              : End



        (/^
        (?=.*\d)                //should contain at least one digit
        (?=.*[a-z])             //should contain at least one lower case
        (?=.*[A-Z])             //should contain at least one upper case
        [a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters

        $/)

Example:-   /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/

回答by Rujoota Shah

I use the following script for min 8 letter password, with at least a symbol, upper and lower case letters and a number

我使用以下脚本输入最少 8 个字母的密码,至少包含一个符号、大小写字母和一个数字

function checkPassword(str)
{
    var re = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
    return re.test(str);
}

回答by ChaosPandion

Don't try and do too much in one step. Keep each rule separate.

不要试图一步做太多。将每个规则分开。

function validatePassword() {
    var p = document.getElementById('newPassword').value,
        errors = [];
    if (p.length < 8) {
        errors.push("Your password must be at least 8 characters");
    }
    if (p.search(/[a-z]/i) < 0) {
        errors.push("Your password must contain at least one letter."); 
    }
    if (p.search(/[0-9]/) < 0) {
        errors.push("Your password must contain at least one digit.");
    }
    if (errors.length > 0) {
        alert(errors.join("\n"));
        return false;
    }
    return true;
}

回答by Valentino Pereira

Regex for password:

密码正则表达式

/^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[a-zA-Z!#$%&? "])[a-zA-Z0-9!#$%&?]{8,20}$/

/^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[a-zA-Z!#$%&? "])[a-zA-Z0-9!#$%&?]{8,20}$/

Took me a while to figure out the restrictions, but I did it!

我花了一段时间才弄清楚这些限制,但我做到了!

Restrictions: (Note: I have used >>and <<to show the important characters)

限制:(注意:我使用了>><<来显示重要的字符)

  1. Minimum 8 characters {>>8,20}
  2. Maximum 20 characters {8,>>20}
  3. At least one uppercase character (?=.*[A-Z])
  4. At least one lowercase character (?=.*[a-z])
  5. At least one digit (?=.*\d)
  6. At least one special character (?=.*[a-zA-Z >>!#$%&? "<<])[a-zA-Z0-9 >>!#$%&?<< ]
  1. 最少 8 个字符 {>>8,20}
  2. 最多 20 个字符 {8,>>20}
  3. 至少一个大写字符 (?=.*[A-Z])
  4. 至少一个小写字符 (?=.*[a-z])
  5. 至少一位 (?=.*\d)
  6. 至少一个特殊字符 (?=.*[a-zA-Z >>!#$%&? "<<])[a-zA-Z0-9 >>!#$%&?<< ]

回答by Mustafa Gen?

If you check the length seperately, you can do the following:

如果单独检查长度,您可以执行以下操作:

var regularExpression  = /^[a-zA-Z]$/;

if (regularExpression.test(newPassword)) {
    alert("password should contain atleast one number and one special character");
    return false;
}