简体   繁体   English

按下输入时的Javascript Masking

[英]Javascript Masking on key press of input

I am working on a custom masking input text box for a ssn. 我正在为ssn创建自定义遮罩输入文本框。 When the users enters in there SSN I have the value saved in a global variable. 当用户在此处输入SSN时,我会将值保存在全局变量中。 I have a span next to the text box if they click on the span the value becomes masked. 如果他们单击跨度,则文本框旁边会有一个跨度,该值将被掩盖。 Ex 555-55-5555 to xxx-xx-5555. 例如555-55-5555至xxx-xx-5555。 Now I want the user to be able to edit the value from this state For example, 22x-xx-5555 and I want it to save for the current value of the box 225-55-5555. 现在,我希望用户能够从此状态编辑值,例如22x-xx-5555,并希望将其保存为框225-55-5555的当前值。 Any suggestions? 有什么建议么? Right now I am passing the real value var conversion 555-55-5555 and turning it into an array and capturing the keypress of the new value var currentValue also turning it to an array. 现在,我正在传递实值var转换555-55-5555并将其转换为数组,并捕获新值var currentValue的按键,还将其转换为数组。 After I have an if statement comparing the lengths meaning the changed the value. 在我有一条if语句比较长度意味着改变值后。 Not sure what to do after. 不知道以后该怎么办。

currentMask(realValue) {
    var self = this;
    self.element.addEventListener('keyup', e => {
        if (self.element.getAttribute('type') === 'ssn') {
            var keycode = (e.which) ? e.which : e.keyCode;
            if ((keycode >= 48 && keycode <= 57) || keycode === 8 || keycode === 8 || keycode === 9 || keycode === 37 || keycode === 39) {
                var conversion = realValue.split("");
                var currentValue = self.element.value.split("");
                if (conversion.length === currentValue.length) {
                    var finale = conversion.concat(currentValue);
                }
                return true;
            }
            e.preventDefault();
            return false;
        }
    });
}

Is this what you were after? 这是你所追求的吗?

Here we are using the native 'password' type that masks the input. 在这里,我们使用掩盖输入的本机“密码”类型。 Upon clicking the show textbox, it will simply convert that input into a text type which will reveal the value. 单击显示文本框后,它将简单地将该输入转换为将显示该值的文本类型。

https://jsfiddle.net/zoo67k86/ https://jsfiddle.net/zoo67k86/

HTML 的HTML

<input id="ssn" type="password" placeholder="ssn">
Show: <input id="show" type="checkbox">

JS JS

document.getElementById('show').addEventListener('click', function(e){
    if(e.target.checked){
    document.getElementById('ssn').setAttribute('type', 'text');
  } else{
    document.getElementById('ssn').setAttribute('type', 'password');
  }
});

Alternatively, you could have four input boxes and detect the blur event to set the previous input boxes to the password type. 或者,您可以有四个输入框并检测模糊事件,以将先前的输入框设置为密码类型。

https://jsfiddle.net/zoo67k86/1/ https://jsfiddle.net/zoo67k86/1/

HTML 的HTML

<div id="ssn">
  <input id="ssn1" data-order="1" maxlength="4">
  <input id="ssn2" data-order="2" maxlength="4">
  <input id="ssn3" data-order="3" maxlength="4">
  <input id="ssn4" data-order="4" maxlength="4">
</div>

JS JS

var ssns = document.getElementsByTagName('input');

for(var i = 0; i < ssns.length; i++){
    ssns[i].addEventListener('blur', function hideEarlierSibling(e){
    var earlierInput = e.target.previousElementSibling;
    if(earlierInput){
        earlierInput.setAttribute('type', 'password');
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM