javascript常用正则验证

限制31之内的数字

/^[1-9]$|^[12][0-9]$|^3[01]$/

格式为2020-06-06

/^[12]\d{3}\-(0[1-9]|1[0-2])\-(0[1-9]|[12][0-9]|3[01])$/

邮箱6~18个字符,可使用字母数字下划线,字母开头

/^[a-zA-Z]\w{5,17}@[a-zA-Z0-9]+?\.(com|net)$/

手机号中间四位隐藏

var tel="13311112222";
console.log(tel.replace(/^(\d{3})(\d{4})(\d{4})$/,"$1****$3")); //133****2222

正则验证密码 (包括数字大小写字母)8-16位

/^(?=\D+[0-9])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$/

验证密码的强度

<input type="text"><span></span>


<script>
        init();
        function init(){
           var input=document.querySelector("input");
           var span=document.querySelector("span");
           input.span=span;
            input.addEventListener("input",inputHandler);
        }

        function inputHandler(e){
            if(this.ids) return;
            this.ids=setTimeout(function(input){
                clearTimeout(input.ids);
                input.ids=0;
                getRightText(input);
            },500,this)
        }

        function getRightText(input){
          if(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{8,16}$/.test(input.value)){
                 input.span.textContent="最强";
                input.span.style.color="green";
           }else if(/^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$|^(?=.*\d)(?=.*[A-Z])[a-zA-Z0-9]{8,16}$|^(?=.*\d)(?=.*[a-z])[a-zA-Z0-9]{8,16}$/.test(input.value)){
                input.span.textContent="中级";
                input.span.style.color="#FFCC00";
            }else if(/^\d{8,16}$|^[a-z]{8,16}$|^[A-Z]{8,16}$/){
                input.span.textContent="初级";
                input.span.style.color="red";
            }else{
                input.span.textContent="错误";
                input.span.style.color="blue";
            }
        }
</script>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44157964/article/details/106593221