jquery only allow input float number

前端 未结 13 1580
既然无缘
既然无缘 2020-12-01 06:28

i\'m making some input mask that allows only float number. But current problem is I can\'t check if multiple dots entered. Can you check those dots and prevent it for me?

相关标签:
13条回答
  • 2020-12-01 07:20
    <input type="text" data-textboxtype="numeric" />
    <script>
        $(document).on('keydown', '[data-textboxtype="numeric"]', function (e) {
            // Allow: backspace, delete, tab, escape, enter and . and -
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190, 109, 189]) !== -1 ||
                // Allow: Ctrl+A
                (e.keyCode == 65 && e.ctrlKey === true) ||
                // Allow: home, end, left, right, down, up
                (e.keyCode >= 35 && e.keyCode <= 40)) {
                // let it happen, don't do anything
                return true;
            }
            // Ensure that it is a number and stop the keypress
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
                return false;
            }
            return true;
        });
    </script>
    
    0 讨论(0)
  • 2020-12-01 07:22
    $('.number').keypress(function(event){
                if($.browser.mozilla == true){
                      if (event.which == 8 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 9 || event.keyCode == 16 || event.keyCode == 46){
                            return true;
                      }
                }
                if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
                    event.preventDefault();
                  }
                    });
    

    This works in all browsers.

    0 讨论(0)
  • 2020-12-01 07:24

    I think you guys have missed the left right arrows, delete and backspace keys.

     $('.number').keypress(function(event) {
    
         if(event.which == 8 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46) 
              return true;
    
         else if((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57))
              event.preventDefault();
    
    });
    
    0 讨论(0)
  • 2020-12-01 07:25

    I think everybody forgot the case of pasting text with the mouse, in which you can't detect the keystrokes, because there's none. Here's another approach I have been working on.

    // only integer or float numbers (with precision limit)
    // example element: <input type="text" value="" class="number" name="number" id="number" placeholder="enter number" />
    
    $('.number').on('keydown keypress keyup paste input', function () {
    
        // allows 123. or .123 which are fine for entering on a MySQL decimal() or float() field
        // if more than one dot is detected then erase (or slice) the string till we detect just one dot
        // this is likely the case of a paste with the right click mouse button and then a paste (probably others too), the other situations are handled with keydown, keypress, keyup, etc
    
        while ( ($(this).val().split(".").length - 1) > 1 ) {
    
            $(this).val($(this).val().slice(0, -1));
    
            if ( ($(this).val().split(".").length - 1) > 1 ) {
                continue;
            } else {
                return false;
            }
    
        }
    
        // replace any character that's not a digit or a dot
    
        $(this).val($(this).val().replace(/[^0-9.]/g, ''));
    
        // now cut the string with the allowed number for the integer and float parts
        // integer part controlled with the int_num_allow variable
        // float (or decimal) part controlled with the float_num_allow variable
    
        var int_num_allow = 3;
        var float_num_allow = 1;
    
        var iof = $(this).val().indexOf(".");
    
        if ( iof != -1 ) {
    
            // this case is a mouse paste (probably also other events) with more numbers before the dot than is allowed
            // the number can't be "sanitized" because we can't "cut" the integer part, so we just empty the element and optionally change the placeholder attribute to something meaningful
    
            if ( $(this).val().substring(0, iof).length > int_num_allow ) {
                $(this).val('');
                // you can remove the placeholder modification if you like
                $(this).attr('placeholder', 'invalid number');
            }
    
            // cut the decimal part
    
            $(this).val($(this).val().substring(0, iof + float_num_allow + 1));
    
        } else {
    
            $(this).val($(this).val().substring(0, int_num_allow));
    
        }
    
        return true;
    
    });
    
    0 讨论(0)
  • 2020-12-01 07:25

    One-more plugin, based on Carlos Castillo answer

    https://github.com/nikita-vanyasin/jquery.numberfield.js

    Adds method to jQuery object:

    $('input.my_number_field').numberField(options);
    

    where options is (you can pass any or no options):

    {
        ints: 2, // digits count to the left from separator
        floats: 6, // digits count to the right from separator
        separator: "."
    }
    
    0 讨论(0)
  • 2020-12-01 07:27

    Below Code I am allowing only Digits and Dot symbol.
    ASCII characters number starts in 47 and ends with 58 and dot value is 190.

       $("#Experince").keyup(function (event) {
            debugger
    
            if ((event.which > 47
                && event.which < 58) ||event.which== 190) {
                 if ($("#Experince").val().length > 3) {
    
            }
            } // prevent if not number/dot
            else {
                 $("#Experince").val($("#Experince").val().slice(0, -1))
            }
    
        });
    
    0 讨论(0)
提交回复
热议问题