【问题标题】:How to use input mask in Javascript for Mac ID & Phone Number如何在 Javascript 中为 Mac ID 和电话号码使用输入掩码
【发布时间】:2021-11-09 19:53:12
【问题描述】:

我想从一个输入字段中过滤电话号码和 Mac id...如果字符串以 '00' 开头,则在每两个带有字符的整数之后放置一个冒号,如果它以任何其他数字开头,则没有放置冒号在两个数字之后...

var dynamicMask = new IMask(document.getElementById('user'), {
    mask: [{
      mask: '0*:**:**:**:**:**'
    },
    {
      mask: /^\S*@?\S*$/
    }
  ]
})


<script src="https://unpkg.com/imask"></script>
<div class="form-group">
  <label>Mac id or phone</label>
  <input type="text" class="form-control" name="user" id="user" placeholder="Enter ..." />
</div>

【问题讨论】:

    标签: javascript imaskjs


    【解决方案1】:

    根据值允许多种格式有点棘手。电话号码没有真正的标准(除了 00 或 + 表示国际,0 表示国家和 1-9 表示提供者/位置的开头),也可能长度为 12,它们几乎不依赖于国家/提供者/目标的位置。

    我建议根据输入长度/值使用两个不同的输入字段而不是两种格式。

    但这样的事情会做到:

    <input type="text" id="user">
    <script>
      /*
       * This allows:
       
       @0123456789af,  @01:23:45:67:89:af and 01:23:45:67:89:af 
          threaded as mac address
       
       any other input like:
       00:00:00:00:00 or 01:23:45:67:89:az
         handled as phone number
         (removes any non numeric value on focusout)
     */
      IMask(user, {
        mask: /[@:0-9a-f]/gi, // allow anything
        prepare(char) {
          // allow only @, :, 0-9 and a-f for input
          // this allows: @[a-f0-9]{12} values to be entered or 00:00:00:00:00:00...
          return char.replace(/[^@:0-9a-f]/i, '');
        },
        commit(value, masked) {
          // leading @ will be removed
          // check if it contains already a : and has a length of 6 sets with 2 byte length each
          if (value.split(':').length == 6 && value.split(':').every(p => p.length == 2) && !value.startsWith('@')) {
            // add an @ leading for the text below
            value = '@' + value;
          }
          value = value.replace(/:/g, '');
          const dummy = document.createElement("input");
          // required else call for IMask() on dummy won't work
          document.body.appendChild(dummy);
          dummy.setAttribute("value", value.startsWith('@') ? value.substr(1) : value);
          // length must be 12 and should not start with 00
          if (/^@[0-9a-f]{12}$/i.test(value)) {
            // remove the leading @ that was added before or by the user and add : formation
            masked._value = IMask(dummy, {
              mask: '**:**:**:**:**:**'
            })._value;
          } else {
            // filter only numbers
            masked._value = IMask(dummy, {
              mask: /.*/,
              prepare(str) {
                return str.replace(/[^0-9]/, '');
              }
            })._value;
          }
          dummy.remove();
        }
      });
    </script>
    

    【讨论】:

    • 它不起作用...不可能创建两个字段...有两种格式 macid= 00:5F:56:7B:34:76 & number = 8765876875 我想过滤这些两种格式
    • 如果你的mac地址以00开头,去掉!value.startsWith("00")勾选。但这会产生我提到的问题,即每个长度为 12 的电话号码都将算作一个 mac 地址。
    • 电话号码以随机数字开头
    • 没有。 - 我已经提到过。电话号码也可以以 00 或 0[1-9] 或 [1-9] 开头。
    • 如果你不能为不同的值添加另一个输入字段,客户端复选框来确定输入的值怎么样?喜欢[ ] phone number 并根据是否选中更改 IMask 格式。
    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2016-10-14
    • 2020-06-06
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    相关资源
    最近更新 更多