How to prevent user from entering decimals?

后端 未结 3 1051
北恋
北恋 2020-12-19 04:02

I\'ve got an order page on my site. Some order items can have decimal quantities, some can\'t.

What\'s the best way to prevent the user from entering decimal quanti

3条回答
  •  一整个雨季
    2020-12-19 04:18

    Here's a little jQuery that prevents non-numerical inputs:

    $(function() {
        $('input').bind('keyup', function(event) {
            var currValue = $(this).val();
    
            if(currValue.search(/[^0-9]/) != -1)
            {
                // Change this to something less obnoxious
                alert('Only numerical inputs please');
            }
    
            $(this).val(currValue.replace(/[^0-9]/, ''));
        });
    });
    

提交回复
热议问题