• 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    
    <p>输入数字并点击验证按钮:</p>
    
    <input id="id1" type="number" min="100" max="300" required>
    <button onclick="myFunction()">验证</button>
    
    <p>如果输入的数字小于 100 或大于300,会提示错误信息。</p>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var inpObj = document.getElementById("id1");
        if (inpObj.checkValidity() == false) {
            document.getElementById("demo").innerHTML = inpObj.validationMessage;
        } else {
            document.getElementById("demo").innerHTML = "输入正确";
        }
    }
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    
    <p>输入数字并点击验证按钮:</p>
    
    <input id="id1" type="number" max="100">
    <button onclick="myFunction()">验证</button>
    
    <p>如果输入的数字大于 100 ( input 的 max 属性), 会显示错误信息。</p>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var txt = "";
        if (document.getElementById("id1").validity.rangeOverflow) {
            txt = "输入的值太大了";
        } else {
            txt = "输入正确";
        }
        document.getElementById("demo").innerHTML = txt;
    }
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    
    <p>输入数字并点击验证按钮:</p>
    <input id="id1" type="number" min="100" required>
    <button onclick="myFunction()">验证</button>
    
    <p>如果输入的数字小于 100 ( input 的 min 属性), 会显示错误信息。</p>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var txt = "";
        var inpObj = document.getElementById("id1");
        if(!isNumeric(inpObj.value)) {
            txt = "你输入的不是数字";
        } else if (inpObj.validity.rangeUnderflow) {
            txt = "输入的值太小了";
        } else {
            txt = "输入正确";
        }
        document.getElementById("demo").innerHTML = txt;
    }
    
    // 判断输入是否为数字
    function isNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
    </script>
    
    </body>
    </html>

  • 相关阅读:
    quagga源码学习--BGP协议的初始化
    Golang pprof heap profile is empty
    python requests 配置超时及重试次数
    SVN: bdb: BDB1538 Program version 5.3 doesn't match environment version 4.7
    OpenSSL Command-Line HOWTO
    树莓派保卫战--防止SSH暴力破解
    Mac OS X Tips
    Git Tips
    SQL分组多列统计(GROUP BY后按条件分列统计)
    Show Linux Package Sort By Size
  • 原文地址:https://www.cnblogs.com/tszr/p/10942896.html
Copyright © 2020-2023  润新知