In this example, we will example how to allow only 10 numbers in textbox using jQuery. Allow only 10 numbers is need when you get mobile number.
<!DOCTYPE html> <html> <head> <title>Allow only 10 numbers in textbox using jQuery</title> </head> <body> <form action=""> <label for="phone">Phone</label> <input type="text" name="phone" id="phone" placeholder="Enter phone" maxlength="10"> <span id="phoneError"></span> <br><br> <input type="button" value="Submit" id="submitForm"> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <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; } }); }); </script> </body> </html>
I hope you understand how to allow only 10 numbers in jQuery and it can help you..