JS正则表达式验证密码是否包含字母,数字,符号中至少两种
时间: 2024-01-13 18:03:29 浏览: 114
以下是一个JS正则表达式,用于验证密码是否包含字母、数字、符号中至少两种:
/^(?=.*[a-zA-Z])(?=.*\d|\W).+$/
解释:
^:字符串的开始
(?=.*[a-zA-Z]):必须包含至少一个字母
(?=.*\d|\W):必须包含至少一个数字或符号
.+:可以包含任意字符,长度至少为1
$:字符串的结尾
使用方法:
let password = "abc123#";
let regex = /^(?=.*[a-zA-Z])(?=.*\d|\W).+$/;
if (regex.test(password)) {
console.log("密码符合要求");
} else {
console.log("密码不符合要求");
}
输出结果:
密码符合要求
相关问题
js邮箱正则表达式验证
这段代码使用了JavaScript正则表达式来验证邮箱格式是否正确。正则表达式是一种用于匹配字符串的模式,可以用来验证输入的数据是否符合特定的格式要求。在这个例子中,使用了一个正则表达式来验证邮箱格式是否正确。具体来说,这个正则表达式的含义是:以字母或数字开头,后面可以跟任意个字母、数字、下划线或连字符,然后是一个@符号,接着是一个或多个字母、数字或连字符,最后是一个点和两到四个字母。如果输入的邮箱符合这个格式,就会弹出一个提示框,显示“邮箱格式正确”,否则会显示“邮箱格式不正确”。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)