javascript 数字验证

2018-12-12  本文已影响0人  不简单的码农

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

</head>

<body>

<h1>JavaScript 数字验证</h1>

<p>只能输入数字:</p>

<input id="numb" >

<button type="button" onclick="myFunction()">提交</button>

<p id="demo"></p>

<script>

    function myFunction() {

        var x, text;

        // 获取 id="numb" 的值

        x = document.getElementById("numb").value;

        console.log(x);

        // 如果输入的值 x 不是数字或者为空  则提示

        if (isNaN(x)  ) {

            text = "请不要乱输,输入数字";

        } else if ( x.length ===0){//判断是否有输入内容

            text = "请输入内容";

        }

        else {

            text = "正确";

        }

      document.getElementById("demo").innerHTML = text;//八提示文本内容显示出来

    }

</script>

</body>

</html><!DOCTYPE html>

上一篇 下一篇

猜你喜欢

热点阅读