jQuery 不同卡片类型的掩码

jQuery mask for different card types

我有一个信用卡输入,它的掩码应该在输入更改事件后动态变化(例如,它的默认掩码是“9999 9999 9999 9999”,但对于“37”,它应该更改为 AMEX 格式“9999 999999 99999” ").

我打字的时候没问题,但是我 copy/paste 的时候格式不正确。

$('input').on('input', function (e) {
  var value = $(this).inputmask('unmaskedvalue');

    $('input').inputmask({
      mask: (value.substr(0,2) === '37' || value.substr(0,2) === '34') ? '9999 999999 99999' : '9999 9999 9999 9999'
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/inputmask/4.0.9/jquery.inputmask.bundle.min.js"></script>

<input>

<p>
Try switching between the these two (paste first, then second then first again):
</p>
<ul>
  <li>5100000000404390</li>
  <li>370000000024291</li>
</ul>

知道哪里错了吗?

我只是跳进去阅读文档 https://github.com/RobinHerbots/Inputmask

这是目前我能提供的帮助

更新: 使用 alternator

$('#cc').inputmask({
  mask: '(3(4|7)99 9{6} 9{5}|3999 9{4} 9{4} 9{4}|9{4} 9{4} 9{4} 9{4})'
}).change(function(){
  let value = $(this).val().substr(0,2);
  $('#vv').inputmask({
    mask: (value==34||value==37)?'9{4}':'9{3}'
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/inputmask/4.0.9/jquery.inputmask.bundle.min.js"></script>

<input id="cc"/>
<input id="vv"/>
<p>
Try switching between the these two (paste first, then second then first again):
</p>
<ul>
  <li>5100000000404390</li>
  <li>370000000024291</li>
</ul>

您没有补偿掩码长度的变化。只需将您的 AMEX 掩码更改为 9999 999999 99999[9]

$('input').on('input', function (e) {
 var value = $(this).inputmask('unmaskedvalue');

   $('input').inputmask({
     mask: (value.substr(0,2) === '37' || value.substr(0,2) === '34') ? '9999 999999 99999[9]' : '9999 9999 9999 9999'
  });
});