简体   繁体   中英

How to insert hyphens into a phone number input?

I have a JavaScript which auto hyphen a user input on a text field for a phone number. It works find on iPhone but on android the hyphen show up and the cursor also get to the right place (after the hyphen) but when user type the number it display it before the hyphen?

What is this issue related to?

Here is my script:

if((input.value.length == 3) || (input.value.length == 7) {
    input.value = input.value + "-";
    input.setSelectionRange(input.value.length,input.value.length);
}

Try this:

if((input.value.length == 3) || (input.value.length == 7)) {
  setTimeout(function() {
    input.value = input.value + "-";
    input.setSelectionRange(input.value.length,input.value.length);
  }, 10);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM