[读码时间] 判断数字是否为两位数

说明:代码取自网络,注释为笔者学习时添加!

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>判断数字是否为两位数</title>
    <style>
        body{
            font:12px/1.5 Arial;/*行高18px*/
            text-align:center;
        }
        .f-text{
            width:50px;
            border:1px solid #ccc;/*灰色*/
            background:#f0f0f0;/*灰白色*/
            font-family:inherit;
            padding:3px;
            margin-right:10px;
        }
    </style>
    <script>
        window.onload = function () {
            var aInput = document.getElementsByTagName("input");
            var aSpan = document.getElementsByTagName("span")[0];
            var i = 0;

            aInput[0].onkeyup = function () {
                this.value = this.value.replace(/[^\d]/, "");//正则的意思是所有输入的非数字字符都会被替换为空,所以输入的时候你会看见你输了一个字母,立马又消失了。。
            }
            aInput[1].onclick = function () {
                (aInput[0].value == "") ?
                alert("请输入数字") :
                alert(/^\d{2}$/.test(parseInt(aInput[0].value)) ? "√ 是两位数" : "这是" + aInput[0].value.length + "位数");
                aInput[0].value = "";
            }
        };
    </script>
</head>
<body>
    <!--两个input元素,一个输入数字,一个为按钮-->
    <input type="text" class="f-text" /><input type="button" value="是否为两位数" />
</body>
</html>
View Code

 

posted @ 2017-02-25 03:03  sx00xs  阅读(475)  评论(0编辑  收藏  举报