开发者

How to insert hyphens into a phone number input?

开发者 https://www.devze.com 2023-03-08 02:15 出处:网络
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 cur开发者_运维问答sor also get to the right pla

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 cur开发者_运维问答sor 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消