Код: Выделить всё
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 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
Подробнее здесь: https://stackoverflow.com/questions/597 ... ar-masking