Restrict input to number only on pasting

后端 未结 7 782
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 21:54

I have an input filed with a class called restrict-numbers which i want to restrict the entered characters to only accept numbers, i used the following code which is great b

7条回答
  •  悲哀的现实
    2021-01-18 22:49

    $(".numbersOnly").bind('paste', function(e) {
            var self = this;
            setTimeout(function(e) {
                var val = $(self).val();
                if (val != '0') {
                    var regx = new RegExp(/^[0-9]+$/);
                    if (!regx.test(val)) {
                        $(".numbersOnly").val("");
                    }
                    $(this).val(val);
                }
            }, 0);
        });
    

提交回复
热议问题