// 限制8位数字+2位小数点 并且小于10000000.00

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="user" name="user">
    <div></div>
    <script>

        // 限制8位数字+2位小数点 10000000.00
        let div = document.querySelector("div");
        let o_val = null;
        document.querySelector("[name='user']").
            addEventListener("keyup", function () {
                let _val = this.value;
                let _reg = /^([1-9]\d{0,2})\.*(\d{0,2})$/;
                // _val.replace(_reg,search=>{

                //    return this.value=_val.match(_reg)[1];
                // })
                let m_val = _val.match(_reg);
                if (m_val != null&&parseFloat(m_val)<=100) o_val = m_val;
                let k_val = o_val != null ? o_val["input"] : "";
               
                this.value=k_val;
                div.innerHTML = k_val;
            })
     
    </script>
</body>

</html>

  

posted on 2021-08-29 18:04  水行者  阅读(84)  评论(0编辑  收藏  举报

导航