刚接触正则就被这个玩意儿吸引了,实在是强大。随便写了个验证手机号的代码。

btw:https://regex101.com/这个网站对学习正则真的非常好用。

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>testing</title>
        <style>
            
        </style>
    </head>
    <body>
        <label for="phoneNumber">PhoneNumber</label>
        <input id="phoneNumber" type="text" name="phoneNumber" placeholder="insert here">
        <button type="submit" id="verifyBtn">verify</button>
        <button type="reset" id="resetBtn">reset</button>
        <label id="info">notification</label>
<script>
        var verifyBtn = document.getElementById("verifyBtn");
        verifyBtn.addEventListener("click", verify);
        var resetBtn = document.getElementById("resetBtn");
        resetBtn.addEventListener("click", reset);
        
        function verify(){
            var pattern = /^(1.[0-9]{9})$/;
            var txt = document.getElementById("phoneNumber").value;
            if(pattern.test(txt)){
                document.getElementById('info').innerText = "verification passed";
            }else{
                 document.getElementById('info').innerText = "insert your correct phone number please";
                 document.getElementById("verifyBtn").disabled = true;
            }
        }
        
        function reset(){
            document.getElementById("phoneNumber").value = "";
            document.getElementById("verifyBtn").disabled = false;
        }
</script>
    </body>
</html>

JS菌
6.4k 声望2k 粉丝