例如:1234567890-> 123-456-7890
答案 0 :(得分:1)
您可以使用简单的正则表达式执行此操作。匹配3位数,3位数,4位数并插入连字符。
const input = '1234567890';
const formatted = input.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
console.log(formatted);
答案 1 :(得分:0)
尝试一下,希望对您有所帮助!
<input id="phoneField" onKeyUp="phoneNumberConverter()">
<script>
function phoneNumberConverter()
{
var number = document.getElementById("phoneField").value;
if (number.length == 10)
{
var number = number.substring(0, 3) + "-" + number.substring(3, 6) + "-" + number.substring(6, 10);
document.getElementById("phoneField").value = number;
}
}
</script>