zoukankan      html  css  js  c++  java
  • js怎么限制文本框input只能输入数字

    1、说明

    本篇文章介绍怎么使用js限制文本框只能输入数字

    2、HTML代码

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript">
            function validationNumber(e, num) {
                var regu = /^[0-9]+.?[0-9]*$/;
                if (e.value != "") {
                    if (!regu.test(e.value)) {
                        alert("请输入正确的数字");
                        e.value = e.value.substring(0, e.value.length - 1);
                        e.focus();
                    } else {
                        if (num == 0) {
                            if (e.value.indexOf('.') > -1) {
                                e.value = e.value.substring(0, e.value.length - 1);
                                e.focus();
                            }
                        }
                        if (e.value.indexOf('.') > -1) {
                            if (e.value.split('.')[1].length > num) {
                                e.value = e.value.substring(0, e.value.length - 1);
                                e.focus();
                            }
                        }
                    }
                }
            }
    
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <input type="text" onkeyup="validationNumber(this,0)" name="id1" value="" />
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    【Linux】——sleep无法正常休眠
    【网络——Linux】——IPMI详细介绍【转】
    【Linux】——ctags
    10086 8元套餐
    建置 区域划分
    几何
    孙武
    监察委
    python 内置函数
    Oracle cursor and table
  • 原文地址:https://www.cnblogs.com/net064/p/10243950.html
Copyright © 2011-2022 走看看