-
Notifications
You must be signed in to change notification settings - Fork 18
/
puremask.min.js
7 lines (7 loc) · 1.13 KB
/
puremask.min.js
1
2
3
4
5
6
7
/* ============================================
* Pure Mask JS: puremask.js v1.0.3
* https://romulobrasil.com
* Copyright (c) 2016-2023 Rmulo Brasil
* ============================================
*/
const PureMask={logs:{element:"PureMask.js: Please declare the element that will receive the mask.",mask:"PureMask.js: data-mask was not declared on the following tag: "},maskFunctions:{"#":c=>/\d/.test(c),A:c=>/[a-z]/i.test(c),N:c=>/[a-z0-9]/i.test(c),X:c=>!0},format(element,placeholder){const els=document.getElementsByClassName(element);els?Array.from(els).forEach(el=>{el.dataset.mask?placeholder&&(el.placeholder=el.dataset.mask):(console.log(this.logs.mask),console.log(el)),el.addEventListener("keydown",e=>{8!=e.keyCode&&46!=e.keyCode&&this.applyMaskToValue(el,el.dataset.mask)})}):console.log(this.logs.element)},applyMaskToValue(el,mask){let formattedText="",currentChar,maskChar;for(let i=0,isValid=!0;isValid&&i<mask.length-1;++i)currentChar=el.value.charAt(i),maskChar=mask.charAt(i),this.maskFunctions[maskChar]?this.maskFunctions[maskChar](currentChar)?formattedText+=currentChar:isValid=!1:formattedText+=maskChar;el.value=formattedText}};