Explain Codes LogoExplain Codes Logo

Is there a float input type in HTML5?

html
input-types
validation
user-interface
Alex KataevbyAlex Kataev·Aug 19, 2024
TLDR

There's no dedicated float type in HTML5, but float values can be accommodated with <input type="number" step="any">:

<input type="number" step="any">

With this, any floating-point numbers can be entered, offering maximum precision.

Precision Tuning via step

Using the step attribute, we can tightly control the precision of the input. For instance, an exact value like 0.01 is perfect for unit currency, sidestepping potential rounding trip-ups:

<input type="number" step="0.01">

If you need to constrain your values to positives only, simply throw in a min attribute:

<input type="number" step="0.01" min="0">

Be aware that setting step impacts validation; an input must be a multiple of the step value from any minimum point defined to be considered valid.

Covering your data's six

Although type="number" works with floating-point numbers, it's important to remember it doesn't natively restrict user input to only numbers. Robust validation — involving client-side checks — contributes to a safer user interface. Never underestimate, however, the power of server-side validation.

Juggling browser idiosyncrasies

The behavior of floating-point numbers may fluctuate between different browsers. Testing the number input across multiple platforms ensures a consistent user experience. Remember that some browsers might not play nice with step="any", potentially tweaking how the input behaves. Therefore, checking up on the compatibility of your work is crucial for avoiding browser-based tantrums.

Handcrafting input controls

At times, type="number" may not offer the nuanced control you might need. Pivoting to type="text" and creating a tailored validation for floats could be a great game plan.

Consider validating European-style decimals, where commas play the decimal role:

function validateFloatInput(event) { const validChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '.']; if (!validChars.includes(event.key) && event.keyCode !== 8) { // Never forget about the Backspace, the unsung hero. event.preventDefault(); } } document.querySelector('input[type="text"]').addEventListener('keypress', validateFloatInput);

This approach tolerates numbers (0-9), periods (.), commas (,), and even the backspace key for those inevitable human errors, capturing various decimal formats in the wild.

Feature boosting with libraries

By marrying supplementary functionality from helpful libraries like jQuery UI, you can construct complex input fields that house sliders, spinners, and localized input conventions. With this level of refinement, you can build intuitive user interfaces that resonate with international users.

Sometimes, you might find yourself needing fixed increments such as 0.1 or 0.5 — setting a proper step value ensures users can only punch in numbers that play by these rules:

<!-- Allows increments of 0.5, because why not? --> <input type="number" step="0.5">

Offering a default value might even help steer users in the right direction, avoiding any initial confusion:

<!-- Includes a default value, a blanket in the night --> <input type="number" step="0.1" value="22.33">

Nip any excessive input attempts in the bud by using a max attribute like max="100".