Aadhaar遮罩

时间:2020-01-14 15:41:33

标签: javascript jquery

我正在尝试检测在 UIView.transition(with: bannerImageView, duration: 0.3, options: [.transitionCrossDissolve], animations: { [weak self] in self?.exampleImageView.image = newImage }) 字段上按下后退按钮的情况。我已经尝试过inpute.key(在移动Chrome浏览器中为e.which)。我该如何工作?在桌面上可以正常工作。

undefined
jQuery(function($) { // DOM ready and $ alias secured
  let aadhaar = "";
  let aadhaarStack = [];
  let maskStack = [];
  let flag = 0;

  $('#aadhaar').on('input', function(e) {
    let key = e.which || this.value.substr(-1).charCodeAt(0);
    console.log("here also")
    
    if (flag === 1) {
      console.log("here")
      aadhaarStack.pop();
      maskStack.pop();
    } else {
      key = String.fromCharCode(key);
      if (aadhaarStack.filter(i => i !== " ").length <= 11 && !isNaN(key)) {
        if (aadhaarStack.length > 1 && (aadhaarStack.filter(i => i !== " ").length) % 4 === 0) {
          aadhaarStack.push(" ");
          aadhaarStack.push(key);
          maskStack.push(" ");
          if (aadhaarStack.filter(i => i !== " ").length > 8) {
            maskStack.push(key);
          } else {
            maskStack.push("X");
          }
        } else {
          aadhaarStack.push(key);
          if (aadhaarStack.filter(i => i !== " ").length > 8) {
            maskStack.push(key);
          } else {
            maskStack.push("X");
          }
        }
      }
    }

    updateUi();
  });

  function updateUi() {
    setTimeout(function() {
      aadhaar = maskStack.join("");
      $('#aadhaar').val(aadhaar);
    }, 100);
  }

  $('#aadhaar').on('keydown', function(e) {
    alert(e.key);
    let key = e.which || this.value.substr(-1).charCodeAt(0);
    if (key === 8 || key === 46 || e.key === 'Backspace') {
      flag = 1;
    } else {
      flag = 0;
    }
    console.log("first here")
  })
});

这是JSBin链接https://jsbin.com/rogepevutu/1/edit?html,js,console,output

1 个答案:

答案 0 :(得分:1)

实际上,由于该解决方案不可靠,因此您无需处理后推。 我的解决方案是尝试比较prev和current的长度,然后在此基础上执行任务。

jQuery(function($) { // DOM ready and $ alias secured
  let aadhaar = "";
  let aadhaarStack = [];
  let maskStack = [];
  let flag = 0;

  $('#aadhaar').on('input', function(e) {
    let key = e.which || this.value.substr(-1).charCodeAt(0);    
    if (this.value.length < aadhaarStack.length) {
      aadhaarStack.pop();
      maskStack.pop();
    } else {
      key = String.fromCharCode(key);
      if (aadhaarStack.filter(i => i !== " ").length <= 11 && !isNaN(key)) {
        if (aadhaarStack.length > 1 && (aadhaarStack.filter(i => i !== " ").length) % 4 === 0) {
          aadhaarStack.push(" ");
          aadhaarStack.push(key);
          maskStack.push(" ");
          if (aadhaarStack.filter(i => i !== " ").length > 8) {
            maskStack.push(key);
          } else {
            maskStack.push("X");
          }
       } else {
         aadhaarStack.push(key);
         if (aadhaarStack.filter(i => i !== " ").length > 8) {
         maskStack.push(key);
       } else {
         maskStack.push("X");
       }
    }
  }
}

  updateUi();
});

function updateUi() {
  setTimeout(function() {
    aadhaar = maskStack.join("");
    $('#aadhaar').val(aadhaar);
  }, 100);
 }
});