简体   繁体   English

如何检查字符串是否包含至少一个数字,字母和既不是数字也不是字母的字符?

[英]How do I check if a string contains at least one number, letter, and character that is neither a number or letter?

The language is javascript. 语言是javascript。

Strings that would pass: 将通过的字符串:

JavaScript1* JavaScript1 *

Pu54 325 5454 325

()9c ()9C

Strings that would not pass: 不会通过的字符串:

654fff 654fff

%^(dFE %^(DFE

I tried the following: 我尝试了以下方法:

var matches = password.match(/\d+/g);
if(matches != null)
 {
        //password contains a number
        //check to see if string contains a letter
        if(password.match(/[a-z]/i))
        {
            //string contains a letter and a number
        }
 }

You can use Regex: 您可以使用正则表达式:

I took it from here: Regex for Password 我从这里获取: 密码的正则表达式

var checkPassword = function(password){
    return !!password.match(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #+=\(\)\^?&])[A-Za-z\d$@$!%* #+=\(\)\^?&]{3,}$/);
};

I use this Regex: 我使用此正则表达式:

Minimum 3 characters at least 1 Alphabet, 1 Number and 1 Special Character: 至少3个字符,至少1个字母,1个数字和1个特殊字符:

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

This regex will enforce these rules: 此正则表达式将执行以下规则:

At least one English letter, (?=.*?[A-Za-z]) 至少一个英文字母(?=。*?[A-Za-z])

At least one digit, (?=.*\\d) 至少一位数字(?=。* \\ d)

At least one special character, (?=. [$@$!% #+=()\\^?&]) Add more if you like... 至少一个特殊字符(?=。 [$ @ $!% #+ =()\\ ^?&])如果愿意,可以添加更多...

Minimum length of 3 characters (?=. [$@$!% #?&])[A-Za-z\\d$@$!%* #+=()\\^?&]{3,} include spaces 最小长度为3个字符(?=。 [$ @ $!% #?&])[A-Za-z \\ d $ @ $!%*#+ =()\\ ^?&] {3,}包含空格

If you want to add more special characters, you can add it to the Regex like I have added '(' (you need to add it in two places). 如果要添加更多特殊字符,可以将其添加到正则表达式中,就像我添加了“(”一样(您需要在两个位置添加它)。

And for those of you who ask yourself what are those two exclamation points, here is the answer: What is the !! 对于那些问自己这两个感叹号是什么的人,这是答案: 什么! (not not) operator in JavaScript? (不是)JavaScript中的运算符?

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

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