1. 程式人生 > >JS-regExp-正則表達式匹配2-6的數字

JS-regExp-正則表達式匹配2-6的數字

技術分享 oct cti itl code utf exp button pro

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
 </head>
 <body>
  <script>
        function testReg(){
            var regExp = /^\d{2,6}$/g;
            var input = prompt("請輸入2-6位的數字:");
            if(regExp.test(input)){
                alert("格式正確");
            }else{
                alert("格式錯誤");
            }
        }
    </script>
    <button onclick="testReg()">測試正則表達式</button>
 </body>
</html>

技術分享圖片

JS-regExp-正則表達式匹配2-6的數字