gpt4 book ai didi

javascript - jQuery 或 Javascript 自动格式化电话掩码

转载 作者:行者123 更新时间:2023-11-30 07:04:38 24 4
gpt4 key购买 nike

我有一个 Rails 应用程序,我正在将 text_field 记录为 string 这是一个电话号码,我希望在以下掩码中自动格式化 xxx-xxx-xxxx

我在搜索 Stack Overflow 时发现了这个 auto-format-phone-number-in-jquery但是 OP 正在寻找格式化 xxx-xxx-xxx 的掩码,我需要弄清楚如何制作 xxx-xxx-xxxx

的掩码

我从这个答案中借用了一个片段,它确实有效,但再次格式化为 xxx-xxx-xxxx

$(function(){
$('#call_caller_phone').keyup(function()
{
this.value = this.value.replace(/(\d{3})\-?/g,'$1-');
});
});

谁能帮我格式化 xxx-xxx-xxxx 掩码?

最佳答案

我相信这可能会奏效。

$(function(){
$('#call_caller_phone').keyup(function()
{
this.value = this.value.replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'$1-$2-$3');
//alert ("OK");
});
});
#call_caller_phone {
color: #fff;
background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="call_caller_phone" type="text">

关于javascript - jQuery 或 Javascript 自动格式化电话掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26412623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com