【问题标题】:Decimal Validation allowing two digits after the Decimal Point on Keypress Event through Javascript十进制验证允许通过Javascript在按键事件上的小数点后两位数
【发布时间】:2012-11-25 17:22:25
【问题描述】:

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

EX: 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 中使用。 :(

【问题讨论】:

  • 为什么需要keypress?顺便说一句,在你的代码中你有keydown
  • 如果您在 Keypress 上尝试此代码将无法正常工作:)

标签: javascript asp.net decimal


【解决方案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" />

【讨论】:

  • 我们可以在 Keypress 事件中这样做吗?>
  • 看起来不错 Sander!所以这个方法会评估用户何时要按下无效字符,从而不允许它们。可以对用户隐藏它吗?
  • 验证现在被破坏了..它允许输入多个十进制字符:(
  • 好的,它现在可以防止两个小数分隔符
  • 非常感谢哥们!!完美!! :)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2013-03-18
    • 2012-09-21
    • 1970-01-01
    • 2019-05-24
    • 2012-03-22
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多