当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS 验证密码 不能为空,必须含有数字、字母、特殊字符,长度在8-12位

JS 验证密码 不能为空,必须含有数字、字母、特殊字符,长度在8-12位

2017年12月12日  | 移动技术网IT编程  | 我要评论

废话不多说了,直接给大家贴代码了,具体代码如下所示:

checkpassword = function(v){
var numasc = 0;
var charasc = 0;
var otherasc = 0;
if(0==v.length){
return "密码不能为空";
}else if(v.length<8||v.length>12){
return "密码至少8个字符,最多12个字符";
}else{
for (var i = 0; i < v.length; i++) {
var asciinumber = v.substr(i, 1).charcodeat();
if (asciinumber >= 48 && asciinumber <= 57) {
numasc += 1;
}
if ((asciinumber >= 65 && asciinumber <= 90)||(asciinumber >= 97 && asciinumber <= 122)) {
charasc += 1;
}
if ((asciinumber >= 33 && asciinumber <= 47)||(asciinumber >= 58 && asciinumber <= 64)||(asciinumber >= 91 && asciinumber <= 96)||(asciinumber >= 123 && asciinumber <= 126)) {
otherasc += 1;
}
}
if(0==numasc) {
return "密码必须含有数字";
}else if(0==charasc){
return "密码必须含有字母";
}else if(0==otherasc){
return "密码必须含有特殊字符";
}else{
return true;
}
}
};

以上所述是小编给大家介绍的js 验证密码 不能为空,必须含有数字、字母、特殊字符,长度在8-12位,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网