Managing the backspace function when using libphonenumber's AsYouTypeFormatter

I've been attempting to integrate the google-libphonenumber's AsYouTypeFormatter into a basic input field on a web form. For each key pressed by the user, I feed it into the inputDigit method. However, I've encountered an issue where when the user hits the backspace key, google-libphonenumber doesn't correctly remove the last digit and simply adds "Backspace" to the phone number. Could it be that I'm incorrectly implementing the AsYouTypeFormatter? Does it lack support for handling backspaces? If so, which approach should I take in dealing with backspace key presses?

To see a sample project showcasing this issue, you can visit: https://stackblitz.com/edit/libphonenumber

Below is the code snippet being used:

import { AsYouTypeFormatter } from 'google-libphonenumber';

const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `
  <h1>Libphonenumber Playground</h1>
  <input id="input" type="text">
`;

this.formatter = new AsYouTypeFormatter('us');

const input = document.getElementById('input') as HTMLInputElement;

input.addEventListener('keyup', (event: KeyboardEvent) => {
  console.log(this.formatter.inputDigit(event.key));
});

Answer №1

"checkNumber": function(event) {
    event = (event) ? event : window.event;
    var keyCode = (event.which) ? event.which : event.keyCode;

if ((keyCode > 31 && (keyCode < 48 || keyCode > 57) || keyCode === 46 || keyCode === 13)) {
    //prevent input of non-numeric characters 
    event.preventDefault();
}else{
    //allow numeric character
    //manage phone number formatting.
    this.phoneNumberInput = this.formatter.digitInput(event.key);
}

I have not come across any functions that will adjust the formatter index backwards. (This does not mean they do not exist) To achieve this, I handled the backspace keydown event. Along with all other non-numeric characters. Here is the code snippet I implemented:

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

Sending dynamic boolean model property via AJAX dynamically

I am facing an issue while passing a property from my model to an AJAX form. The boolean value is resolving as "true/false" and causing problems in the process. $.ajax({ url: '/Projects/SearchTable2', type: "GET", data: { sub ...

Creating a Webpack bundle that includes third-party JavaScript files consists of utilizing the appropriate configuration settings

After researching Webpack and checking similar posts on stackoverflow, I'm still struggling to add additional files to my bundle. Currently, I've successfully included my js modules in the bundle.js file, but now I want to incorporate other files ...

Ensuring the website retains the user's chosen theme upon reloading

I have been developing a ToDo App using MongoDB, EJS, and Node JS. My current challenge involves implementing a theme changer button that successfully changes colors when clicked. However, whenever a new item is added to the database, the page reloads caus ...

The isMobile variable from useSettings is failing to update correctly when the window is resized in a Next

I’ve encountered an issue with determining the device type (mobile, tablet, or desktop) in my Next.js application. I’ve utilized the is-mobile npm package to identify the device type and passing this data through settings. However, the isMobile flag fa ...

Can the tab button be used to move to the next field?

Is it possible to navigate to the next field by pressing the tab button? If so, how can we achieve this? Currently, I am utilizing Bootstrap classes col-md-6. Thank you! <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4. ...

Changing Text to Number in JavaScript

Looking to convert the string value of 41,123 into an integer using JavaScript. I attempted parseInt(41,123, 10) and parseFloat methods but haven't received the desired result. The issue seems to be with ParseInt and parseFloat when encountering com ...

"Utilize Azure Storage to easily upload an image file and retrieve its URL for seamless display using the img

I am looking to utilize Azure file storage for storing image files. I have the ability to upload an image file using the code snippet below: ... const uploadBlobResponse = await blockBlobClient.uploadFile('./test.png'); console.log('Blob was ...

Mastering the art of iterating through a JSON response

Looking to populate a combobox with data from the database. Upon accessing mystream.php?theLocation=NewYork, I receive the following JSON response: RESULT {"result": [{"theID":"36"},{"theStream":"0817-05131"},{"theLabel":"hgjbn"},{"theLocation":"NewYork ...

My webpage is experiencing issues with function calls not functioning as expected

I have created a select menu that is integrated with the Google Font API. To demonstrate how it works, I have set up a working version on JSBIN which you can view here. However, when I tried to replicate the code in an HTML page, I encountered some issues ...

Avoid using @2x images on iPad 3 when rendering in HTML5 Canvas

Recently, I developed a game using the HTML5 canvas. However, it seems that the performance of this game on my iPad 3 is not as good as expected. I am speculating that the retina display of the iPad might be the cause of this issue. Is there a possible sol ...

Add the file retrieved from Firestore to an array using JavaScript

Trying to push an array retrieved from firestore, but encountering issues where the array appears undefined. Here is the code snippet in question: const temp = []; const reference = firestore.collection("users").doc(user?.uid); firestore .collec ...

Display a form with hidden input that dynamically updates on any input change

<form id="pricecal"> <input type="text" onchange="calprice()" class="form-control round-corner-fix datepicker" data-provide="datepicker" placeholder="Check in" value="" required /> <input type="text" onchange="calprice()" class="form ...

Compilation failed: Unable to parse due to an unexpected token, ";" expected

I'm encountering an error in my component class after creating a constructor: Component class with constructor: import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; constructor(p ...

Resolving Text Animation Flickering Issues in CSS and React

After successfully creating this animation using CSS animations and React, I encountered a flickering issue. The cause of this problem is unclear to me, it could be due to my usage of setInterval or potential issues with the keyframes in my CSS animations. ...

Updating form validation by altering input value

I'm currently utilizing JavaScript regular expressions for form validation in my project. After successfully completing the validation without any errors, upon clicking the submit button, I want the form to be submitted and change the submit value to ...

I am facing difficulties in installing node packages using npm install

Trying to install node packages using npm, but encountering an issue. When I run the command, the output is: up to date, audited 356 packages in 7s found 0 vulnerabilities I have listed the dependencies in my package.json file like so: "dependencies& ...

Assignment on Ionic's Cascading Style Sheets classes

As I work on styling my app, I find myself struggling with the extra CSS classes that are added when I preview the source in a running app. It's particularly confusing when all I want to do is change the menu header background. In my app.html file, I ...

Show a pop-up window when a button is clicked, just before the page redirects

Is there a way to display a popup message before redirecting to another page when clicking on a button? Here's the scenario: <a href="addorder.php?id=<? echo $row01['id']; ?>" ><button id="myButton" class="btn btn-primary btn ...

Is there a way to assign a value to a select option in Angular 7?

The issue arises when attempting to modify the value, as it consistently displays the initial option selected. Here is the structure of my HTML code: <div class="col-lg-4 col-md-4 col-sm-12 mt-4"> <div class="form-group"& ...

The appearance of the pop-up mask is displayed at lightning speed

Check out the demo here Here is the HTML code: <div class="x"> </div> <input class="clickMe" type="button" value="ClickMe"></input> This is the JS code: $(".clickMe").click( function() { ...