Beyond Basics: Handling Numeric Inputs Like a Pro with ``
What is
According to MDN Docs Definition: elements of type number are used to let the user enter a number. They include built-in validation to reject non-numerical entries.
So basically, it is a feature for enhancing User Experience on our crappy websites and web apps. While it is a nice feature for simplifying numeric inputs, it has a couple of behaviors that might not be obvious at first but we should be aware of as a web developer.
These behaviors are related to:
- Virtual Keyboard
- Scientific Notations
Virtual Keyboard
A virtual keyboard is a keyboard that appears on your screen, usually in touch input devices like mobile phones, tablets/ipads and some assistive technologies.
<input id="numeric-input" type="number" >
Just using the type="number" might seem all right at first, but it has an issue when using a virtual keyboard. It might open a full-key keyboard instead of a numeric keyboard, which can hurt user experience and input accuracy.
`" />
Solution inputmode
While the virtual keyboard issue might not occur on newer OS and browser versions, it is still a good idea to use the inputmode attribute as it hints at the type of data that might be entered by the user while editing the element or its contents.
This allows browsers to display an appropriate virtual keyboard for all versions and devices, improving the application's overall user experience (UX).
inputmode can have many values, but for our use case with numeric inputs we should use one of these two values:
1.numeric
<input id="numeric-input" type="number" inputmode="numeric" >
The input type number accepts any rational value. If you are not accepting fractional values and only accepting whole numbers, using a numeric value should be preferred for such scenarios.
2.decimal
<input id="numeric-input" type="number" inputmode="decimal" >
If you are accepting fractional numbers, use decimal value as it will show a virtual keyboard containing the digits and decimal separator for the user's locale.
Always prefer using decimal value in your input unless you are not accepting fractional values.
`" />
Scientific Notations
Many of us might have forgotten or not know that e or E are valid numeric values in scientific notations.
`" />
Yes, e or E means "exponent of ten" in scientific notations, and is used to represent very large or small values. e.g.
1e5 = 100000 or 1e-5 = 0.00001
The HTML input type number was designed with flexibility in mind, and it supports this scientific notation, allowing for the entry of exponents (e.g., 1e5). While this is not an issue in itself and can be useful in some contexts, it can become problematic when the input should only consist of basic numbers.
There are two ways to restrict the input to only numbers and not allow the scientific notations.
- Using Regex ? pattern.
- Using Javascript (my preferred method).
Preventing Default Behaviour Using Javascript
In most scenarios, I don't want the users to enter the 'e' or 'E' notations, as they aren't needed, and allowing them can cause some unexpected issues.
There are many ways of achieving this, but my favorite method is to prevent the 'e' or 'E' key from being registered. We can achieve this using the keydown event and preventDefault.
element.addEventListener("keydown", (e) => { if (["e", "E"].includes(e.key)) { // you can also add "+" and "-" if you want to prevent them from being registered. e.preventDefault(); } });
I particularly like this method because of three reasons:
- It provides a clear message to anyone reading our code later that we don't want to register the e or E keys.
- It also allows for a better UX because instead of changing the values we are not registering the key itself.
- The type of keydown event is KeyboardEvent which gives us direct access to the key values improving the overall DX of the solution.
Not All Numeric Input Needs to be Input Type Number
Sometimes we can instinctively reach for an input type number if we see an input requirement that accepts numeric input but sometimes input type number is not the right solution for such scenarios.
A few examples of such scenarios are:
Credit Card/Debit Card: When we have an input field for Credit/Debit Card, instead of using input type number it is better to use input type text with input mode of numeric and validation using your preferred method, as a credit card can have leading zero values and some browsers might not allow that, also we might want to add some card specific validations or allow spacing between a group of four numbers (as it is printed on the card).
Postal Code: While most countries have numeric postal codes, some countries like Canada could have alphanumeric codes, it is important to use solutions according to your need, it could be of input type number or input type text.
Mobile Numbers: You should always use the input type of input="tel" with a regex pattern suitable for your use case.
Conclusion
Since modern HTML provides some really nice utilities, some things might feel very trivial to implement like input type number, however as developers, we should always pay attention to these utilities and how we can utilize them to create even better user experiences.
If you liked this article please leave a reaction and if you disliked something you can leave your feedback in the comments.
I like to connect and talk with people about tech and the events surrounding it. You can connect with me on Twitter/X and LinkedIn.
You can also follow me on Dev.to to get a notification whenever I publish a new article.
The above is the detailed content of Beyond Basics: Handling Numeric Inputs Like a Pro with ``. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
