天天看點

js 正則去除指定的單詞

以企業郵箱為例:@後面不能是qq   126   163  188 gmail   yahoo   sina   hotmail  suhu   sogu  等單詞。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function email1(){

            var Rex=/\w[-\w.+]*@(?!qq\b|163\b|126\b|188\b|gmail\b|yahoo\b|sina\b|hotmail\b|suhu\b|sogu\b)([\w][\w]+\.)+[A-Za-z]{2,14}/;
            var email=document.getElementById("email").value;
            var username_err=document.getElementById("err_email");
            if(email==""){
                username_err.innerHTML="郵箱不能為空";
                username_err.style.color="red";
                return false
            }else{
                if(!Rex.test(email)){
                    console.log(email);
                    username_err.innerHTML="郵箱格式不正确";
                    username_err.style.color="red";
                    return false
                }else{
                    console.log(email);
                    username_err.innerHTML="";
                    return true
                }

            }
        }
    </script>
</head>
<body>
<div class="formControls col-xs-4 col-sm-5">
    <input type="text" id="email" placeholder="請輸入企業郵箱"  name="email" value="" class="input-text" required oninput="email1()" >
    <span id="err_email"></span>
</div>
</body>
</html>      
js