This code restricts the insertion of characters other than numbers in the input field. When I insert a value into the input field, I check the value with a regular expression, if the condition is true, the value will be set, or else I will empty the input value. In my case, I had to limit other characters besides numbers, you can change the regex based on your requirement
$(".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); });
source share