简体   繁体   English

十进制验证允许通过Javascript在按键事件上的小数点后两位

[英]Decimal Validation allowing two digits after the Decimal Point on Keypress Event through Javascript

I need to restrict the user to enter two digits after the decimal point using pure Javascript on Key press event.Please help.. 我需要限制用户在按键事件上使用纯Java小数点后两位输入数字。请帮助。

EX: 123.45 ->Correct 123.45.6 ->Incorrect 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>

I tried this way,but need to have it in Keypress. 我尝试过这种方式,但需要在Keypress中使用。 :( :(

Maybe you can just correct the value using parseFloat: 也许您可以使用parseFloat来更正值:

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

I changed it to onchange because otherwise it would prevent you from typing a . 我将其更改为onchange,因为否则它将阻止您键入。 at all. 完全没有 This however means it will only validate once when you blur the input. 但是,这意味着仅当您模糊输入时,它将仅验证一次。

EDIT 编辑


Like this then? 这样吗?

JS: 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: HTML:

<input onkeypress="return validateFloatKeyPress(this, event)" type="text" />

I also having same problem.This code has solved my problem.It's not only foramt yous decimal number but also will eliminate blank spaces. 我也遇到了同样的问题,这段代码解决了我的问题,它不仅为您的十进制数字加了密码,而且还消除了空格。 Try this.As in my condition i was allowing user to enter '+' or '-' so i check for this validation also.I have called this function onblur event.Hope this help u, 尝试此操作。在我的情况下,我允许用户输入“ +”或“-”,因此我也检查了此验证。我将此函数称为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 Source: HTML来源:

<asp:TextBox ID="txtspherical" runat="server" OnBlur="javascript:checkforvalidation();">
        </asp:TextBox>

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

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