英文:
how to add zero to phone number in mask using jquery?
问题
我正在尝试在我的电话号码掩码中添加一个0。我希望用户看到0,然后输入其余的电话号码。我如何使用jQuery实现这个目标?
jQuery(function ($) {
$("#phoneNumber").mask("0(999) 999-9999");
});
用户需要首先看到0,不能删除它。例如,必须像这样:0(555) 555-5555
。当用户输入电话号码时,0必须已经在其中。
英文:
I'm trying to add a 0 for my phone number mask. I want user to see 0 and then type the rest of their telephone number. How can I do this with jQuery?
jQuery(function ($) {
$("#phoneNumber").mask("9(999) 999-9999");
});
User needs to see 0 at first and can't remove it.
For example, it has to be like 0(555) 555-5555
. 0 already needs to be in there when user types the phone number.
答案1
得分: 3
你可以使用下面的解决方法:
'translation': {
0: null
}
思路是删除插件中指定的默认行为 0: {pattern: /\d/}
(如果需要,你可以重新分配此模式给任何其他数字或字母)。
之后,mask
插件会在必要时插入前导的 0
。
$(function() {
// 'Basic Usage Examples'
$('.phone').mask('0 (999) 999-9999', {
'translation': {
0: null
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<div>
<div class="col">
<label>Phone</label>
<input type="text" class="phone" />
</div>
</div>
英文:
You can use the workaround below
'translation': {
0: null
}
The idea is that you remove default behavior '0': {pattern: /\d/}
specified in plugin (you can reassign this pattern to any other number or letter if you need)
After this, mask
plugin just insert leading 0
if that missed.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
// 'Basic Usage Examples'
$('.phone').mask('0 (999) 999-9999', {
'translation': {
0: null
}
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<div>
<div class="col">
<label>Phone</label>
<input type="text" class="phone" />
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
以下是翻译好的部分:
如果您在输入中有一个数字0,并且希望用户无法删除这个0,可以像这样做:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
$('#phoneNumber').on('keydown', function(key) {
if (key.which == 8 && $(this).val().length === 1) {
return false;
}
});
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<input type="text" id="phoneNumber" value="0" />
<!-- 结束代码片段 -->
<!-- 不要测试它,但是 -->
您可以只是不让他删除输入字段中的第一个硬编码值,这个值是您想要放在首位的0。您需要通过不让他在0之前输入内容来增强此功能,还需要禁用复制和粘贴等功能。key.which之后的数字8表示当前按下的键的整数值,按下退格键时为8。
英文:
In case you have an input and have a number 0 in it and you want that the user can't remove the 0 you can do something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('#phoneNumber').on('keydown', function(key) {
if (key.which == 8 && $(this).val().length === 1) {
return false;
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<input type="text" id="phoneNumber" value="0" />
<!-- end snippet -->
<strike>Couldn't test it but</strike> You can just don't let him remove the first entry of the input field with an hardcoded value in html which is the 0 you want to stand first. You need to enhance this by don't let him type before the 0 and you need to disable copy & paste for example. The 8 after key.which stands for the current pressed key integer value. Which is 8 on pressing backspace.
答案3
得分: -1
<!-- 开始代码片段:不隐藏,控制台输出,不使用Babel编译 -->
<!-- 使用JavaScript语言 -->
$(document).ready(function () {
$('#phone').mask('N(999) 999-9999', {
placeholder: '0(XXX) XXX-XXXX',
translation: {
'N': {
pattern: /[0]/,
optional: false
}
}
});
})
<!-- 使用HTML语言 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<input type="text" id="phone">
<!-- 结束代码片段 -->
这是您提供的代码片段的翻译部分。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function () {
$('#phone').mask('N(999) 999-9999', {
placeholder: '0(XXX) XXX-XXXX',
translation: {
'N': {
pattern: /[0]/,
optional: false
}
}
});
})
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<input type="text" id="phone">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论