Allow only numeric (0-9) in inputbox | jQuery?
There is no native jQuery implementation for allow numbers and decimal only in Textbox, but you can filter the input values of a Textbox with the following onlyNumeric function. This function suppports backspace, tab, delete, enter, arrows, numbers and keypad numbers only.
jQuery.fn.onlyNumeric =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode e.keyCode 0;
return (
key == 8
key == 9
key == 13
key == 46
key == 110
key == 190
(key >= 35 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
});
});
};

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
jQuery.fn.onlyNumeric =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode e.keyCode 0;
return (
key == 8
key == 9
key == 13
key == 46
key == 110
key == 190
(key >= 35 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
});
});
};
$("#input-1").onlyNumeric();
});
</script>
</head>
<body>
<input type="text" id="input-1">
</body>
</html>
Allow numeric values only | JavaScript
You can use the following JavaScript code for allow numeric values only.
<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" />

Allow numeric values only | JavaScript
<!DOCTYPE html>
<html>
<body>
<h2>Allow numeric values only | JavaScript</h2>
<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" />
</body>
</html>
Related Topics
- How to get input textbox value using jQuery
- Get Selected Dropdown Value on Change Event | jQuery
- How to submit a form using ajax in jQuery
- How can I get the ID of an element using jQuery
- Open Bootstrap modal window on Button Click Using jQuery
- How to select an element with its name attribute | jQuery
- How to get the data-id attribute using jQuery
- How to disable/enable submit button after clicked | jQuery
- How to replace innerHTML of a div using jQuery
- Change the selected value of a drop-down list using jQuery
- How to get the value of a CheckBox with jQuery
- How to wait 'X' seconds with jQuery?
- How to detect enter key press on keyboard | jQuery
- How to dynamically create a div in jQuery?