检查整数和分数长度以使用javascript事件限制输入

时间:2016-09-09 19:19:22

标签: javascript jquery

我正在编写一个javascript函数,它将根据验证集限制用户输入和粘贴。

用户输入可以包含数字,小数和逗号。

这部分完成,下一部分是限制用户输入超过指定的整数r分数。

我尝试过几件事,但无法理解它。



$(function(){
  
  $.fn.checkDecimal = function (integer, fractional) {
return this.each(function () {

    $(this).on("paste", function (event) {

        var val = event.originalEvent.clipboardData.getData('Text'),
                    regex = new RegExp("/^\d{0," + integer + "}(?:[.,]\d{1," + fractional + "})?$/"),
                    test = regex.test(val);

        if (!test) {
            event.preventDefault();
        }
    }).on('keydown', function (event) {

        // Allow: backspace, delete, tab, escape, and enter
        if (event.which == 46 || event.which == 8 || event.which == 9 || event.which == 27 || event.which == 13 ||
            // Allow: Ctrl+V
                (event.ctrlKey == true && (event.which == '118' || event.which == '86')) ||
            // Allow: Ctrl+c
                (event.ctrlKey == true && (event.which == '99' || event.which == '67')) ||
            // Allow: Ctrl+A
            (event.which == 65 && event.ctrlKey === true) ||
            // Allow: home, end, left, right
            (event.which >= 35 && event.which <= 39)) {
            // let it happen, don't do anything
            return;
        }
        else {
            //// Ensure that it is a number and stop the keydown

            //Only allow period, comma and numbers
            if (event.which != 8 &&
                    (event.which != 190 || $(this).val().indexOf('.') != -1 || $(this).val().indexOf(',') != -1) &&
                    (event.which != 188 || $(this).val().indexOf(',') != -1 || $(this).val().indexOf('.') != -1) &&
                    (event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }

                //Only specified numbers after a decimal
            else if (event.which != 8 &&
                        (event.which > 48 || event.which < 57) &&
                        ($(this).val().indexOf('.') != -1) &&
                        ($(this).val().indexOf('.') < $(this).val().length - fractional)) {
                event.preventDefault();
            }
                //Only specified numbers before a decimal
            else if (event.which != 8 &&
                        (event.which > 48 || event.which < 57) &&
                        ($(this).val().length == integer) &&
                         $(this).val().indexOf('.') == -1 &&
                        (event.which != 190 && event.which != 188)) {
                event.preventDefault();
            }
        }

    }).on("keyup", function (event) {

        var val = $(this).val();

        $(this).val(val.replace(',', '.'));

    });
});
}
  
  $(document).ready(function(){
        $('#restrictiveDecimals').checkDecimal(4,3);
  });
  
  
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label>
  Only Numbers, Dot & Comma allowed
  <br>
  But need to restrict input length
</label> 
<input type="text" id="restrictiveDecimals">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这是解决方案。 我初始化了5个整数和2个小数。 如果您输入的数字超过5位而未保留&#39;。&#39; ,代码将放置&#39;。&#39;排在第6位。 运行它,看看..欢呼:)

&#13;
&#13;
$(function() {

    $.fn.checkDecimal = function(integer, fractional) {
        return this.each(function() {

            $(this).on("paste", function(event) {

                var val = event.originalEvent.clipboardData.getData('Text'),
                    regex = new RegExp("/^\d{0," + integer + "}(?:[.,]\d{1," + fractional + "})?$/"),
                    test = regex.test(val);

                if (!test) {
                    event.preventDefault();
                }
            });
            var count=0;
            var no_of_integers = 5;
            var no_of_fractions = 2;
            var int_count = 0;
            var fraction_count = 0;
            $(this).on('keydown', function(event) {
              console.log(count+"-"+int_count+"-"+fraction_count)
                if (int_count == 0 || fraction_count == 0) {
                    if (event.which == 46) {
                        count--;
                        
                    }
                    // Allow: backspace, delete, tab, escape, and enter
                    if (event.which == 46 || event.which == 8 || event.which == 9 || event.which == 27 || event.which == 13 ||
                        // Allow: Ctrl+V
                        (event.ctrlKey == true && (event.which == '118' || event.which == '86')) ||
                        // Allow: Ctrl+c
                        (event.ctrlKey == true && (event.which == '99' || event.which == '67')) ||
                        // Allow: Ctrl+A
                        (event.which == 65 && event.ctrlKey === true) ||
                        // Allow: home, end, left, right
                        (event.which >= 35 && event.which <= 39)) {
                        // let it happen, don't do anything

                        return;
                    } else {
                        //// Ensure that it is a number and stop the keydown


                        //Only allow period, comma and numbers
                        if (event.which != 8 &&
                            (event.which != 190 || $(this).val().indexOf('.') != -1 || $(this).val().indexOf(',') != -1) &&
                            (event.which != 188 || $(this).val().indexOf(',') != -1 || $(this).val().indexOf('.') != -1) &&
                            (event.which < 48 || event.which > 57)) {
                            event.preventDefault();
                        }

                        //Only two numbers after a decimal
                        else if (event.which != 8 &&
                            (event.which < 48 || event.which > 57) &&
                            ($(this).val().indexOf('.') != -1) &&
                            ($(this).val().indexOf('.') >= 0 && $(this).val().indexOf('.') < $(this).val() - fractional)) {
                            event.preventDefault();
                        } else if (event.which == 190) {

                            int_count = count;
                            count = 1;
                        }

else if (count == no_of_integers) {
                                int_count = count;
                                count = 1;
                                document.getElementById("restrictiveDecimals").value = document.getElementById("restrictiveDecimals").value + ".";
								count++;
                            }						else {
                            if (int_count == 0 && count <= no_of_integers) {
                                count++;
                               
                            } else if (int_count != 0 && count < no_of_fractions) {
                                count++;
                              
                            } else if (int_count != 0 && count == no_of_fractions) {
                                fraction_count = count;
                                count == 0;
                            }
                        }
                    }
                }
				else{
				event.preventDefault();
				}
            });

            $(this).on("keyup", function(event) {

                var val = $(this).val();

                $(this).val(val.replace(',', '.'));

            });
        });
    }

    $(document).ready(function() {
        $('#restrictiveDecimals').checkDecimal(4, 3);
    });


});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label>
  Only Numbers, Dot & Comma allowed
  <br>
  But need to restrict input length
</label> 
<input type="text" id="restrictiveDecimals">
&#13;
&#13;
&#13;