天天看點

正則校驗1-65535整數

正則校驗1-65535的表達式:

複制測試:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span{
            margin-left:20px;
        }
    </style>
</head>
<body>
    <div id="one">
        整數輸入校驗(1-65535)<input type="text" id="search" value="">
        <button onclick="check()">送出</button>
    </div>
    <script>
       var str = document.createElement("span")
       var exp=/^([1-9](\d{0,3}))$|^([1-5]\d{4})$|^(6[0-4]\d{3})$|^(65[0-4]\d{2})$|^(655[0-2]\d)$|^(6553[0-5])$/
       function check(){
        var val=document.getElementById("search")
        str.innerHTML=exp.test(val.value)
        setStyle(str,exp.test(val.value))
        document.getElementById("one").appendChild(str)
       }
       function setStyle(dom,bool){
        if(bool){
            dom.style.color="green"
        }else{
            dom.style.color="red"
        }
       }
    </script>
</body>
</html>