如何在使用jQuery的掩码中向电话号码添加零?

huangapple go评论66阅读模式
英文:

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

&#39;translation&#39;: {
    0: null
}

The idea is that you remove default behavior &#39;0&#39;: {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() {
  // &#39;Basic Usage Examples&#39;
  $(&#39;.phone&#39;).mask(&#39;0 (999) 999-9999&#39;, {
    &#39;translation&#39;: {
      0: null
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
  &lt;div class=&quot;col&quot;&gt;
    &lt;label&gt;Phone&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;phone&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的部分:

如果您在输入中有一个数字0,并且希望用户无法删除这个0,可以像这样做:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
$(&#39;#phoneNumber&#39;).on(&#39;keydown&#39;, function(key) {
  if (key.which == 8 &amp;&amp; $(this).val().length === 1) {
    return false;
  }
});

<!-- 语言: lang-html -->
<script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;></script>
<script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js&quot;></script>

<input type=&quot;text&quot; id=&quot;phoneNumber&quot; value=&quot;0&quot; />

<!-- 结束代码片段 -->

<!-- 不要测试它,但是 -->
您可以只是不让他删除输入字段中的第一个硬编码值,这个值是您想要放在首位的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 -->

$(&#39;#phoneNumber&#39;).on(&#39;keydown&#39;, function(key) {
  if (key.which == 8 &amp;&amp; $(this).val().length === 1) {
    return false;
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js&quot;&gt;&lt;/script&gt;

&lt;input type=&quot;text&quot; id=&quot;phoneNumber&quot; value=&quot;0&quot; /&gt;

<!-- 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 () {
    $(&#39;#phone&#39;).mask(&#39;N(999) 999-9999&#39;, {
        placeholder: &#39;0(XXX) XXX-XXXX&#39;,
        translation: {
            &#39;N&#39;: {
                pattern: /[0]/,
                optional: false
            }
        }
    });
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js&quot;&gt;&lt;/script&gt;


&lt;input type=&quot;text&quot; id=&quot;phone&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月21日 22:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定