only allow numbers in text input in js
$(document).ready(function() { $("#myTextBox").inputFilter(function(value) { return /^\d*$/.test(value); // Allow digits only, using a RegExp },"Only digits allowed"); });
Source: stackoverflow.com
javascript only allow numbers
function onlyNumbers(e) { // Check if the user pressed a number if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { // If the user pressed a number, prevent the default action e.preventDefault(); } // Then, add the event listener to the input document.getElementById("input").addEventListener("keypress", onlyNumbers); // If the key pressed is not a number, prevent the default action if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { e.preventDefault(); } // If the key pressed is a number, allow the default action if (e.which == 8 || e.which == 0 || (e.which >= 48 && e.which <= 57)) { return; } } // Call the function onlyNumbers(e);