Which is the proper way of filtering numeric values for a text field?

后端 未结 3 1903
时光取名叫无心
时光取名叫无心 2020-12-19 17:44

I\'m working on a textfield working with the kind of validation that wouldn\'t let you enter other than numeric values. As so, my initial code looked quite simple and simila

3条回答
  •  醉话见心
    2020-12-19 18:10

    this will allow both int. it also removes text if user copy and paste with mouse.

    $(document).ready(function () {
        $('#textfield').bind('keyup blur', function (e) {
            if (e.type == 'keyup') {
                if (parseInt($(this).val()) != $(this).val()) {
                    $(this).val($(this).val().slice(0, $(this).val().length - 1));
                }
            } else if (e.type == 'blur') {
                $(this).val('');
            }
        });
    });
    

提交回复
热议问题