Allow only Numbers in Textbox using jQuery

In this post, we will explain how to allow only numbers in textbox using jQuery. We will get only number using keypress event code. Allow only numbers in textbox in need when you get valid numbers.

<!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=""></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 allow only numbers in textbox using jQuery..

Read Also :  jQuery Get Image Src and Set Image Src