question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not delete phone number in US National format when the input string has 3 characters

See original GitHub issue

I’m using AsYouType to format the input phone number, I got an error when attempting to delete phone number by press Backspace when the input string has 3 characters.

Check out the gif and the code below: asyoutype-error

new AsYouType('US').input(value)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
Morsmetuscommented, Mar 23, 2021

I am was looking for an answer but don’t really like to start applying AsYouType after 3 digits… Here is what I came up with, hope it helps others:

export const formatPhoneNumber = (value?: string | number) => {
    if (!value) return '';
    value = value.toString();
    if (value.includes('(') && !value.includes(')')) {
        return value.replace('(', '');
    }
    return new AsYouType('US').input(value);
};

This is kind of a helper function I use to include in input onchange if input type is phone

8reactions
jharsonocommented, Jul 20, 2018

@nguyennb9 I was able to work around this by only formatting once the character count is over 3 digits. What I believe to be happening is that when you hit the backspace, the field is still evaluating to 3 digits which will add the parenthesis for US format. Adding in a conditional to start formatting after 3 digits have been entered worked well for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Phone Number Formatting — How to Do It, Why It Is Important
You might think that choosing the right format, or ensuring that all customer phone numbers in your database are consistent is unimportant.
Read more >
How do format a phone number as a String in Java?
I tried using DecimalFormat but that doesn't like the hyphen. Probably because it is meant for formatting decimal numbers and not longs. long...
Read more >
E.164 phone number field configuration
How search works: Punctuation and capital letters are ignored. Special characters like underscores (_) are removed.
Read more >
4.3. Validate International Phone Numbers - O'Reilly
^ # Assert position at the beginning of the string. \+ # Match a literal "+" character. (?: # Group but don't capture:...
Read more >
Check whether second string can be formed from characters ...
It contains well written, well thought and well explained computer science and ... Input : str1 = geekforgeeks, str2 = geeks ... Output...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found