简体   繁体   English

Javascript / Jquery验证keyup上的十进制输入

[英]Javascript/Jquery validating decimal input on keyup

I'm trying to find a way to validate a text input on key press, I want to allow numbers only inside my text input including decimals. 我正在尝试找到一种方法来验证按键时的文本输入,我只允许数字输入包含小数的文本。 I was taking the approach of using jQuery.keydown, and checking what the key was and using. 我采用的是使用jQuery.keydown的方法,并检查键的内容并使用它。

input numbers max length 999.999 输入数字最大长度999.999

- -

my result after . 我的结果之后 (point) 3 number it is goog. (要点)3号是goog。 but 99999.999 但是99999.999

I need 999.999,222.222,22.01 -> max length +++.+++ 我需要999.999,222.222,22.01-> 最大长度+++。+++

this my code 这是我的代码

<input type="text" id="spinEdit2" class="aSpinEdit" />

<script type="text/javascript">
var txt = document.getElementById('spinEdit2');
    txt.addEventListener('keyup', myFunc);

    function myFunc(e) {
        var val = this.value;

        var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?[0-9]?|[0-9])/g;
            val = re1.exec(val);
            //console.log(val);
            if (val) {
                this.value = val[0];
            } else {
                this.value = "";
            }

    }
</script>

Thanks 谢谢

How about 怎么样

/^([0-9]{1,3}(?:\.[0-9]{0,3})?)/g

In case you want the details: the {1,3} means the preceding thing can happen from 1 to 3 times. 如果需要详细信息: {1,3}表示前面的事情可能发生1至3次。 The (?:) is a non-captured group -- it's a grouping of the following symbols, but it doesn't capture to any variables like $1. (?:)是一个未捕获的组-它是以下符号的组合,但不会捕获到$ 1之类的任何变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM