废话不多说了,直接给大家贴代码了,具体代码如下所示:
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位,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!