使用javascript更改格式 像这样输入数字,然后在下一个字段中显示如下

使用javascript更改格式 像这样输入数字,然后在下一个字段中显示如下,javascript,html,Javascript,Html,我希望使用JavaScript来完成此操作。我已经尝试了很多次,但没有找到任何成功的方法。请帮助我。您可以使用这个漂亮的轻量级插件 $(“#textfield”).mask(“(999)999-9999”) 输入这样的数字,然后在下一个字段中显示如下 尝试使用oninput,onfocus事件,String.prototype.replace()替换为RegExp/(^\d{3})/匹配前三位数字,/(\s\d{3,3})(?!-{1}$//code>匹配空格字符后的三位数字,而不是输

我希望使用JavaScript来完成此操作。我已经尝试了很多次,但没有找到任何成功的方法。请帮助我。

您可以使用这个漂亮的轻量级插件

$(“#textfield”).mask(“(999)999-9999”)


输入这样的数字,然后在下一个字段中显示如下

尝试使用
oninput
onfocus
事件,
String.prototype.replace()
替换为
RegExp
/(^\d{3})/
匹配前三位数字,
/(\s\d{3,3})(?!-{1}$//code>匹配空格字符后的三位数字,而不是输入字符串的结尾;在
input type=“text”
元素中将
maxlength
属性设置为
14
var input=document.querySelector(“input[name=textfield]”);
input.oninput=input.onfocus=function(){
this.value=this.value
。取代(/(^\d{3})/,“($1)”)
.替换(/(\s\d{3,3})(?!-{1}|$)/,“$1-”)
}
input.focus()

试试这个,用你的代码制作一个演示。。我们能把注意力转移到click@Harsimransingh“我们可以将焦点更改为单击”是
<input name="textfield" type="text" id="textfield" value="6267973685" /> Enter number like this and in next field it will display like this
<input name="textfield2" type="text" id="textfield2" value="(626) 797-3685" />