//文本框监测
<input type="text" id="txt">
<span id="tip"></span>
<script>
/*
长度为6-16
全是数字为弱
有特殊字符为强
其他为中
*/
//给txt绑定事件
var str = "@#$%^&*";
txt.oninput = function () {
//判断长度
if(txt.value.length < 6 || txt.value.length > 16){
return tip.innerHTML = "长度不合法";
}
//判断数字
if(Number(txt.value) == txt.value){
return tip.innerHTML = "密码强度弱";
}
//判断是否含有特殊字符
for(var i = 0; i < str.length; i++){
if(txt.value.indexOf(str[i]) != -1){
return tip.innerHTML = "密码强度强";
}
}
}
</script>
//敏感词过滤
<input type="text" id="txt">
<button id="btn">提交</button>
<span id="tip"></span>
<script>
var arr = ["sb", "fuck", "md", "laji"];
//给btn绑定事件
btn.onclick = function(){
var txtvalue = txt.value;
for(var i = 0; i < arr.length; i++){
txtvalue = txtvalue.replace(arr[i], "*");
}
tip.innerHTML = txtvalue;
}
</script>
//验证码
<span id="code"></span>
<button id="btn">获取验证码</button>
<script>
//给btn绑定事件
btn.onclick = function () {
var str = "";
var num = null;
//获取4个随机数
for(var i = 0; i < 4; i++){
num = parseInt(Math.random() * 74 + 48);
//判断num是否合格
if(num >= 48 && num <= 57 || num >= 65 && num <= 90 || num >= 97 && num <= 122){
str += String.fromCharCode(num);
}else{
i--;
}
}
code.innerHTML = str;
code.style.color = randomColor();
}
//随机颜色
function randomColor(){
var str = "0123456789abcdf";
var color ="#";
for(var i = 0; i < 6; i++){
color += str[parseInt(Math.random() * 16)];
}
return color;
}
原文地址:https://www.cnblogs.com/wenlx/p/11454975.html