How to allow only numeric (0-9) in HTML inputbox using jQuery?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
To create a user-friendly form in an HTML application, ensuring input data quality is crucial. A common requirement is to restrict input in a text box to numeric values only (0-9). Using jQuery, a fast, small, and feature-rich JavaScript library, can simplify this task. Below, we explore how to implement this with detailed steps and examples.
Understanding the Need for Numeric Input Validation
Input validation is vital for many reasons:
- Security: Prevents malicious data from being entered.
- Data Integrity: Ensures the data is in the expected and required format.
- User Experience: Provides immediate feedback to users about invalid entries.
Using jQuery to Restrict Inputs to Numeric Characters
Step 1: Setup Your HTML and jQuery
Firstly, ensure your web page is set up to use jQuery. If it's not already included, you can add it through a CDN (Content Delivery Network) like so:
With jQuery included, you can now add an HTML input box where numeric input will be restricted:
Step 2: Implementing jQuery to Restrict Non-Numeric Characters
To restrict user input to numeric characters, you can bind an event to the input element that reacts every time a user presses a key. Here's how to do it using jQuery:
Explanation:
- The
keydownevent triggers whenever a key is pressed. - The
$.inArray()function checks whether the key code of the key pressed is amongst common non-character keys like backspace (8), tab (9), enter (13), etc., that should be allowed for functional purposes. - We check for modifier keys combined with letters like 'A' (for select all), 'C' (for copy), and 'X' (for cut).
- Finally, numeric keys on the keyboard (both top-row numbers and keypad numbers) are explicitly checked, and other character inputs are prevented using
event.preventDefault().
Summary Table of Allowed Key Codes
| Key | Key Code |
| Backspace | 8 |
| Tab | 9 |
| Enter | 13 |
| Escape | 27 |
| Delete | 46 |
| Ctrl/cmd+A | 65 |
| Ctrl/cmd+C | 67 |
| Ctrl/cmd+X | 88 |
| Home | 35 |
| End | 36 |
| Arrow Left | 37 |
| Arrow Up | 38 |
| Arrow Right | 39 |
| Arrow Down | 40 |
| Numeric keys 0-9 | 48-57 |
| Keypad keys 0-9 | 96-105 |
Enhancing Functionality
To improve the user experience further, consider adding visual feedback or error messages when non-numeric characters are entered. You can also use HTML5 input types like <input type="number">, though these behave slightly differently across browsers and do not restrict key presses.
Implementing numeric input validation with jQuery is an effective way to ensure data quality and enhance security and usability in form inputs.
Related reading
- How to always show scrollbar
- How to apply CSS to iframe?
- How to apply !important using .css()?
- How to apply multiple transforms in CSS?
- How to asynchronously load a google map in AngularJS?
- How to auto generate migrations with Sequelize CLI from Sequelize models?
- How to avoid long nesting of asynchronous functions in Node.js
- How to await an async call in JavaScript in a synchronous function?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.