phpicoder Dec 14, 2021 Jquery

Hi Dev, In this tutorial we will learn how to allow only numbers in textbox using jquery example, This post we implement simple example for input textbox enter only number not allow text and symbol. user key pres and we check this is a number on not if number return true else return false.

We will create a very simple and complete example to allow only number for textbox validation using jquery so you can understand how this example works. Which are as follows.

Example :
<!DOCTYPE html>
  <title>Allow only numbers in textbox using jquery -</title>
  <script src=""></script>

<div class="container">
  <h1>Allow only numbers in textbox using jquery -</h1>
  <label>Enter Mobile Number:</label>
  <input type="text" name="mobile" class="only_numeric" >
  <span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>

<script type="text/javascript">
	$("body").on("keypress", ".only_numeric", function (event){
      	var keyCode = event.which ? event.which : event.keyCode

      	if(!(keyCode >= 48 && keyCode <= 57)){
        	$(".error").css("display", "inline");
        	return false;
        	$(".error").css("display", "none");

I hope it can help you...