1

キープレスイベントで純粋なJavascriptを使用して、ユーザーが小数点以下2桁を入力するように制限する必要があります.助けてください..

例: 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 に変更したのは、そうしないと a を入力できなくなるためです。まったく。ただし、これは、入力をぼかしたときに一度だけ検証されることを意味します。

編集


じゃあこう?

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

私も同じ問題を抱えています。このコードは私の問題を解決しました。10進数をフォーマットするだけでなく、空白も削除します。これを試してみてください。私の状態では、ユーザーに「+」または「-」の入力を許可していたので、この検証もチェックします。この関数を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 に答える