Enter Only Number in Textbox using jQuery

In this example, I will show jQuery enter only number in textbox. In this post you can create enter only numbers in textbox jQuery on keypress event.

Example : Enter only number in textbox

<!DOCTYPE html>
      <title>Allow only numbers in jquery</title>
    <form action="">
        <label for="phone">Phone</label>
        <input type="text" name="phone" id="phone" placeholder="Enter phone" maxlength="10">
        <span id="phoneError"></span>
        <input type="button" value="Submit" id="submitForm">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    $(document).ready(function() {
      $("#phone").keypress(function (e) {
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                $("#phoneError").html("Enter only digits").css('color','red');
                return false;

I hope you understand how to enter only number in input field and it can help you…