26
loading...
This website collects cookies to deliver better user experience
<input type="password">
, it utilizes the logic of input content masking provided by browser. However, it does not meet the requirement of show partial masked content + rest unmasked content without losing original unmasked content.const preInput = this.preInput;
const index = e.target.selectionStart;
const nowValue = e.target.value;
//compare length of previous current input value
const increment = nowValue.length - preInput.length;
if (increment >= 0) {
const newStr = nowValue.slice(index - increment, index);
this.preInput = preInput.slice(0, index - increment) + newStr
preVal.slice(index - increment)
}
else if (increment < 0) {
this.preInput = preInput.slice(0, index) +
preInput.slice(index - increment);
}
if (nowValue.length > 0) {
const len = nowValue.length-2 > 0 ? nowVal.length - 2 : 0;
e.target.value = new Array(len).fill('*').join("") +
nowValue.slice(len);
}
console.log("original content:", this.preInput)
//v-model needs to be indicate in order for input to receive value
<input v-model="hiddenInput" @input="onInput"/>
onInput(e){
const preInput = this.preInput;
const index = e.target.selectionStart;
const nowValue = e.target.value;
const increment = nowValue.length - preInput.length;
if (increment >= 0) {
const newStr = nowValue.slice(index - increment, index);
this.preInput = preInput.slice(0, index - increment) +
newStr + preVal.slice(index - increment)
} else if (increment < 0) {
this.preInput = preInput.slice(0, index) +
preInput.slice(index - increment);
}
if (nowValue.length > 0) {
//define the mask rule, here is to visualize last two digits
const len = nowValue.length-2 > 0 ? nowVal.length - 2 : 0;
e.target.value = new Array(len).fill('*').join("") +
nowValue.slice(len);
}
//update cursor
e.target.setSelectionRange(index, index)
}