1

我需要在按键事件中使用纯 Javascript 限制用户在小数点后输入两位数。请帮助..

例如:123.45 -> 正确 123.45.6 -> 不正确

function checkDec(el) {
    var ex = /^[0-9]+\.?[0-9]*$/;
    if (ex.test(el.value) == false) {
        el.value = el.value.substring(0, el.value.length - 1);
    }
}

<asp:TextBox ID="txttest0" runat="server" onkeydown="checkDec(this);" ></asp:TextBox>

我尝试过这种方式,但需要在 Keypress 中使用它。:(

4

2 回答 2

1

也许您可以使用 parseFloat 更正该值:

<input onchange="this.value = parseFloat(this.value) || ''" type="text" />

我将其更改为 onchange 因为否则它会阻止您输入 . 一点也不。然而,这意味着它只会在您模糊输入时验证一次。

编辑


那这样呢?

JS:

function validateFloatKeyPress(el, evt) {

    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31
        && (charCode < 48 || charCode > 57)) {
        return false;
    }

    if (charCode == 46 && el.value.indexOf(".") !== -1) {
        return false;
    }

    return true;
}

HTML:

<input onkeypress="return validateFloatKeyPress(this, event)" type="text" />
于 2012-11-25T17:26:46.403 回答
0

我也有同样的问题。这段代码解决了我的问题。它不仅可以格式化你的十进制数,还可以消除空格。试试这个。在我的情况下,我允许用户输入“+”或“-”,所以我也检查了这个验证。我已经调用了这个函数onblur事件。希望这对你有帮助,

<script type="text/javascript">
        function checkforvalidation() {
            var txtvalue = document.getElementById('<%=txtspherical.ClientID %>').value;
            var leftstr = "";
            var rightstr = "";
            var tempstr = "";
            var operator = "";
            txtvalue = txtvalue.replace(/\s/g, '');
            document.getElementById('<%=txtspherical.ClientID %>').value = txtvalue;
            if (txtvalue.indexOf(".") != -1) {

                leftstr = txtvalue.split(".")[0];
                rightstr = txtvalue.split(".")[1];
                if (leftstr.indexOf("-") == 0 || leftstr.indexOf("+") == 0) {

                    operator = leftstr.substr(0, 1);
                    tempstr = leftstr.substr(1, leftstr.length - 1);

                    leftstr = ltrim(tempstr, '0');

                    if (leftstr.length == 0) {
                        leftstr = '0';
                    }

                    if (rightstr.indexOf("-") == -1 || rightstr.indexOf("+") == -1) {

                        rightstr = ltrim(rightstr, '0');

                        rightstr = chkdecimalpoints(rightstr);
                        if (operator != null || operator != "") {
                            txtvalue = operator + leftstr + "." + rightstr;
                        }
                        else {
                            txtvalue = leftstr + "." + rightstr;
                        }
                        document.getElementById('<%=txtspherical.ClientID %>').value = txtvalue;
                    }
                    else {
                        document.getElementById('<%=txtspherical.ClientID %>').value = "";
                    }
                }
                else {

                    tempstr = leftstr.substr(0, leftstr.length);
                    leftstr = ltrim(tempstr, '0');
                    if (leftstr.length == 0) {
                        leftstr = '0';
                    }
                    if (rightstr.indexOf("-") == -1 || rightstr.indexOf("+") == -1) {

                        rightstr = rtrim(rightstr, '0');
                        rightstr = chkdecimalpoints(rightstr);
                        txtvalue = leftstr + "." + rightstr;
                        document.getElementById('<%=txtspherical.ClientID %>').value = txtvalue;
                    }
                }
            }
            else if (txtvalue.indexOf("-") == -1 || txtvalue.indexOf("+") == -1) {

                txtvalue = ltrim(txtvalue, '0');
                if (txtvalue.length == 0) {
                    txtvalue = '0';
                }
                if (operator != null || operator != "") {
                    txtvalue = operator + txtvalue + ".00";
                }
                // txtvalue = leftstr + "." + rightstr;
                document.getElementById('<%=txtspherical.ClientID %>').value = txtvalue;
            }
            else if (txtvalue.indexOf("-") == 0 || txtvalue.indexOf("+") == 0) {

                operator = txtvalue.substr(0, 1);
                tempstr = txtvalue.substr(1, leftstr.length - 1);
                txtvalue = alltrim(tempstr, '0');
                if (operator != null || operator != "") {
                    txtvalue = operator + txtvalue + ".00";
                    document.getElementById('<%=txtspherical.ClientID %>').value = txtvalue;
                }
            }
        }

        function chkdecimalpoints(rightstr) {
            if (rightstr.length == 0) {
                rightstr = '00';

                return rightstr;

            }
            else if (rightstr.length == 1) {
                rightstr = rightstr + '0';
                return rightstr;
            }
            else if (rightstr.length > 2) {

                var tempvar = rightstr.substr(2, 1);

                if (tempvar >= 5) {

                    tempvar = parseInt(rightstr.substr(1, 1)) + 1;
                    tempvar = rightstr.substr(0, 1) + tempvar.toString();
                    if (tempvar.length > 2) {
                        tempvar = tempvar.substr(0, 2);
                    }
                    return tempvar;
                }
                else {

                    tempvar = rightstr.substr(0, 2);
                    return tempvar;
                }
            }
            else {
                return rightstr;
            }
        }
        function ltrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
        }
        function rtrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
        }
        function alltrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("^[" + chars + "]+$", "g"), "");
        }

    </script>

HTML 源代码:

<asp:TextBox ID="txtspherical" runat="server" OnBlur="javascript:checkforvalidation();">
        </asp:TextBox>
于 2012-11-25T18:20:51.060 回答