Skip to content

Commit 69cf0a5

Browse files
committed
mm
1 parent c9a44e0 commit 69cf0a5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

micromasks/mm.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(() => {
2+
window.mm = (i, mask) => {
3+
const d = {9: "[0-9]", a: "[a-z]"}, msk = mask.split("");
4+
i.addEventListener("input", handler);
5+
i.addEventListener("focus", handler);
6+
7+
function handler(e) {
8+
if (e.type === "focus" && i.value !== "") return;
9+
let mskd = [],
10+
s = i.selectionStart - 1;
11+
msk.forEach((el, n) => {
12+
if (d[el]) {
13+
let t = new RegExp(d[el], "i").test(i.value.charAt(n));
14+
mskd.push(t ? i.value.charAt(n) : "_");
15+
if (t && s === n && i.value.charAt(n) !== "_") {
16+
s++;
17+
}
18+
} else {
19+
mskd.push(el);
20+
if (s === n) s++;
21+
}
22+
});
23+
i.value = mskd.join("");
24+
i.selectionStart = i.selectionEnd = s < 0 ? 0 : s;
25+
setTimeout(function () {
26+
i.selectionStart = i.selectionEnd = s < 0 ? 0 : s;
27+
}, 0);
28+
}
29+
};
30+
})();

0 commit comments

Comments
 (0)