SweetAlert2 中的数字掩码

Number mask in SweetAlert2

我想在 sweetalert2 的输入中放置一个数字掩码,你能帮我吗? 我的代码:

onClick(btn) {
    let code2_fa = '';
    if (JSON.parse(localStorage.getItem('user')).password.two_factors.is_active) {
      swal({
        title: 'Digite o TOKEN para prosseguir',
        imageUrl: './assets/imgs/Authy.svg',
        imageAlt: 'Logo Authy',
        input: 'text',
        inputPlaceholder: 'Digite o TOKEN...',
        inputAttributes: {
          maxlength: '6',
          autofocus: 'true',
          required: 'true',
        },
        inputValue: code2_fa,
        animation: true,
        allowEnterKey: true,
        inputAutoTrim: true,

      })
  }

我正在使用 Angular6,目的是显示一个模式,以便用户可以输入他们的授权码。

如果你想使用 vanilla javascript,你可以使用 swalonBeforeOpen 属性 为输入的按键事件注册一个处理程序,例如:

onBeforeOpen: () => {
  swal.getInput().onkeypress = (event) => {
    return Number.isInteger(parseInt(event.key))
  }
}

上面的实现是一个非常简单的按键检测器 returns false 如果按下的键不是数字。

可以在 https://three-keeper.glitch.me/

中看到 运行 的实现