Displaying large numbers in HTML input fields in a formatted manner, such as adding a space or separator for

Presenting the user on one device with a string of numbers, like so:

123 879 233 223 211 782 782 

The numbers are displayed in groups of three, as shown above.

Now, the user needs to input these numbers on another device.

I want them to enter the numbers exactly as they see them.

Currently, I am using an

  input type=number pattern='\d*'

I would prefer it to be (pseudo) :

 type=text keyboard=numbers pattern='[0-9]{3}[0-9]{3} ...' 

The number type is beneficial for mobile devices because it brings up the numeric keypad, which is more relevant and user-friendly in this scenario.

1) Is there a way to get a numbers keyboard for a standard text input field?

2) If not, can we format the numbers entered into the numeric input field?

3) If not, should we question the competence of those at W3C responsible for these standards? Instead of just allowing patterns in text inputs, they could have introduced a 'keyboard=numbers' attribute for better usability.

Answer №1

Here is the solution:

I designed a system with two input fields. One of them has the type number and is positioned absolutely, cleverly hidden using opacity.

When you focus on typing in one input field, the hidden number input automatically becomes focused instead.

The numbers entered into the hidden input are then transferred and displayed in the visible text input field.

<input type=text name=guid placeholder='Enter 18 digits' modulo='3' max='18' />


function handleNumberInput(input) {
        var modulo   = parseInt(input.attr('modulo'));
        var max      = parseInt(input.attr('max')) || Infinity;

        var virtualKeyboard = $('<input type=number pattern="\d*" style="opacity:0; position:absolute;">');

        virtualKeyboard.insertBefore(input);

        input.on('focus', function() {
                virtualKeyboard.focus();
        });

        virtualKeyboard.on('input', function fn(){
                var tmp = virtualKeyboard.val(), val = '';

                if ( tmp.length > max ) {
                        virtualKeyboard.val( tmp = tmp.substring(0, max) )
                }

                var i = -1;
                while ( ++i < tmp.length ) {
                        if ( i > 0 && i % 3 === 0 ) {
                                val += ' ';
                        }
                        val += tmp.charAt(i);
                }

                input.val(val);

                // Trigger original event for external listeners 
                input.trigger.apply(input, arguments);
                // Note, the event target passed to trigger is now on the wrong object. To get the right object, simply change it to: 
                // input.trigger.call(input, 'input', arguments);
                // Note that for the latter, attempts to modify the value won't get picked up. 

        });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Real-time audio feed permanently stuck in Chromium-powered web browsers

Here is how the audio element appears. No interaction seems to take place with it. (Experimented on ungoogled chromium, Edge, and an android device using a default Chrome browser) Below is the HTML code for the audio element: <div> <audio co ...

Creating a user-friendly error message on the interface using JavaScript

I have implemented a table that showcases data from the Geonames API. Currently, I am working on creating a function to handle errors when the user forgets to fill in the input fields and then trigger an alert on the interface. The code snippet below sho ...

Struggling to grasp the concept of closures?

Exploring the concept of closure led me to conduct some experiments, and a specific problem caught my attention. Upon running the following code: var hello; hello = 'abc'; test(); function test() { console.log(hello); ...

How can one easily implement a countdown timer in an Ionic 3 app?

Easily create an Ionic3 timer set to 2 minutes that displays countdown like 1:59, 1:58, and finally reaches 00:00. Once the timer hits 00:00, a block of HTML content will be shown. ...

What makes this JQuery function only compatible with post requests?

Why does this JQuery function only work with post and not with get? I don't want to make any changes to my database just to retrieve some information. It seems that when passing parameters, it automatically recognizes type: post in the Ajax call even ...

javascript issue with accessing global variable outside of function despite multiple attempts

Within my JavaScript function, I am able to retrieve a country code and its corresponding geometry based on the country name from a JSON object. This allows me to dynamically add the country border and store the country code when a country name is selected ...

Guide on dynamically displaying a page based on the response from express/mssql middleware

I have developed a full stack application that includes a registration feature which successfully adds data to the database. Now, I am looking for a way to conditionally display the home page based on whether the login credentials are correct. Within my l ...

Ways to stop the browser from ending the connection?

I'm working on a project using node/express/react, built with create-react-app. I'm facing an issue where the browser automatically closes post requests that take longer than 2 minutes. Specifically, in Chrome, the requests are being closed at e ...

Text buttons on the menu are running into each other when displayed on an Android device

After recently completing a redesign of my website, hetoft.com, I am proud to say that I crafted the design and code entirely by hand, with only Dreamweaver as a helpful tool. This was my first experience creating a website with a CSS-based layout, and des ...

Is there a way to alter the footer across all my pages using just one document?

I'm having trouble coming up with a title for my question, so please bear with me. I am working on a Bootstrap website and I want to create a consistent navbar and footer across all pages without duplicating the code in each document. How can I achiev ...

Optimizing Angular.js templates for faster loading using Node.js pre-compilation

As I delve into learning Angular and integrating it with my current Node.js framework, I find myself facing some challenges. Previously, I utilized Handlebars.js as my templating engine in Node.js, where I would build the context on the server side and the ...

After the JavaScript calculation is completed, the following display may show a value of NaN

Check out my JavaScript calculation in action: Live Preview I've encountered an issue with the calculation script. After the initial calculation, it shows a NAN value on subsequent calculations without reloading the page. Is there a way to enable rep ...

The webpage is inaccessible only on mobile Safari, with an error message indicating a lack of internet

Initially, I do not possess an iPad, however, some clients have reported an unusual issue with one of my websites on the iPad. They are unable to access any page on the website, instead encountering a blank page with the error message: "Safari cannot open ...

Activate the ion-navbar button when submitting from the modal page

import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import {ModalPage} from '../modal-page/modal-page'; @Component({ selector: 'page-page2', templateUrl: 'pa ...

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

What is causing the click event handler to only function on the initial page?

Within my code for the "fnInitComplete" : function(oSettings, json), I am utilizing a selector like $('[id^=f_]').each(function (). The data for Datatables is retrieved server-side and "bProcessing":true I am aware that my selectors are only ef ...

Display Numerous Values Using Ajax

Having difficulty showing data from the 'deskripsisrt' table in a modal bootstrap? I have successfully displayed from the 'srtptr' table, but not sure how to proceed with the 'deskripsisrt' table. Here's a snippet from my ...

Tips for setting up npm dependencies in a subfolder

Within the main directory, I have a package.json file that contains this command: "install": "cd packages/my-package && yarn". Every time I execute yarn run install, my intention is for it to enter into the specified package, set up the node modul ...

I am looking to execute a MySQL query using a value entered into a text box

$query="SELECT * FROM table1 where name='Vidya'"; My text field is ready to capture input. <input type='hidden' value='<?php echo $query;?>' id='getsearchquery' name='getsearchquery'> When I ...

Discovering the smallest and largest values in an object literal using JavaScript

I am looking to extract the minimum value from an object literal and utilize it in AngularJS, as shown below: scope.data = { 'studentName' : "Anand", 'socialStudies' : "98", 'english' : "90", 'math' ...