How to allow only numbers in Textbox using Jquery?
When “How to allow only numbers in Textbox using Jquery?” moves through design, development and browser testing, time tracking software can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.
For API changes, browser support and current usage patterns, compare the snippet with the official jQuery website before adopting it in production.
Hi Guys,
In this example,I will learn you how to use allow only number in textbox using jquery.you can simply and easy to use validation for only number using jquery.
If you need to add jquery validation for your textbox like textbox should accept only numbers values on keypress event. you can also use keyup or keypress event to allow only numeric values in textbox using jquery.
I want to write very simple and full example so you can understand how it is working this example. you can also check demo.let' see bellow full example:
Example 1 :
<!DOCTYPE html>
<html>
<head>
<title>JQuery - Allow only numeric values in Textbox - nicesnippets.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1>JQuery - Allow only numeric values in Textbox - nicesnippets.com</h1>
<label>Enter Value:</label>
<input type="text" name="myValue" class="allow-numeric" >
<span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".allow-numeric").bind("keypress", function (e) {
var keyCode = e.which ? e.which : e.keyCode
if (!(keyCode >= 48 && keyCode <= 57)) {
$(".error").css("display", "inline");
return false;
}else{
$(".error").css("display", "none");
}
});
});
</script>
</body>
</html>
Example 2 :
<!DOCTYPE html>
<html>
<head>
<title>JQuery - Allow only numeric values Textbox - nicesnippets.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1>JQuery - Allow only numeric values in Textbox - nicesnippets.com</h1>
<label>Enter Value:</label>
<input type="text" name="myValue" class="allow-numeric" >
<span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".allow-numeric").on("keypress keyup blur",function (event) {
$(this).val($(this).val().replace(/[^\d].+/, ""));
if ((event.which < 48 || event.which > 57)) {
$(".error").css("display", "inline");
event.preventDefault();
}else{
$(".error").css("display", "none");
}
});
});
</script>
</body>
</html>
It will help you....
- How to Show Loading Spinner in JQuery?
- How to Create Ajax Submit Form Using jQuery?
- How To Get Current Page URL, Path, Host and hash using jQuery?
- How To Get, Set and Delete Div Background Image jQuery?
- JQuery Remove All Unwanted Whitespace From String Example
- Body Background Video Using Vide Jquery Example
- JQuery UI Autocomplete Example
- JQuery Split String By Comma Into Array Example