This question already has an answer here:
这个问题在这里已有答案:
- Regular Expression for Password Strength Validation 6 answers
密码强度验证的正则表达式6个答案
I'm working on a strength password test, and I'm trying to check if the password has upper, and lower, case characters. I'm using two regular expressions, and they're almost working; with the code just here:
我正在进行强度密码测试,我正在尝试检查密码是否包含大写和小写字符。我正在使用两个正则表达式,它们几乎正在工作;使用此处的代码:
var upper = false;
var lower = false;
var upperCase= new RegExp('[^A-Z]');
var lowerCase= new RegExp('[^a-z]');
if (password.match(upperCase )){
upper = true;
}
if (password.match(lowerCase)){
lower = true;
}
When I'm typing numbers, or just a digit, like "1", upper and lower become true.
当我输入数字或只是一个数字,如“1”时,上下变为真。
I'm not really good with regex, did I made a mistake?
我对正则表达式不是很好,我犯了错误吗?
2 个解决方案
#1
0
Take a look at the following which uses test to return a Boolean of whether the password contains an upper case and a lower case. You will see how it tests both regexes against various passwords.
看看以下使用test来返回密码是否包含大写和小写的布尔值。您将看到它如何针对各种密码测试两个正则表达式。
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('[a-z]');
function test(password) {
return [ upperCase.test(password), lowerCase.test(password)];
}
console.log(test("Abc"));
console.log(test("abc"));
console.log(test("123"));
#2
0
Unless you are paid per line of code; you could simply write:
除非你按行代码付款;你可以简单地写:
var upper = password.match(/[A-Z]/);
var lower = password.match(/[a-z]/);
This also fixes the bug in your code where you use [^]
which negates the match (aka; your upperCase
regexp match non-uppercase chars)
这也修复了你的代码中使用[^]的错误,它会使匹配失效(aka;你的upperCase regexp匹配非大写字符)
#1
0
Take a look at the following which uses test to return a Boolean of whether the password contains an upper case and a lower case. You will see how it tests both regexes against various passwords.
看看以下使用test来返回密码是否包含大写和小写的布尔值。您将看到它如何针对各种密码测试两个正则表达式。
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('[a-z]');
function test(password) {
return [ upperCase.test(password), lowerCase.test(password)];
}
console.log(test("Abc"));
console.log(test("abc"));
console.log(test("123"));
#2
0
Unless you are paid per line of code; you could simply write:
除非你按行代码付款;你可以简单地写:
var upper = password.match(/[A-Z]/);
var lower = password.match(/[a-z]/);
This also fixes the bug in your code where you use [^]
which negates the match (aka; your upperCase
regexp match non-uppercase chars)
这也修复了你的代码中使用[^]的错误,它会使匹配失效(aka;你的upperCase regexp匹配非大写字符)