-
Notifications
You must be signed in to change notification settings - Fork 0
/
masker.min.js
7 lines (7 loc) · 5.42 KB
/
masker.min.js
1
2
3
4
5
6
7
/*!
* MaskerJS v1.22 - Vanilla Javascript mask plugin to input form elements
* Copyright 2019-2024 Silvio Delgado (https://github.com/silviodelgado)
* Licensed under MIT (https://opensource.org/licenses/MIT)
* https://github.com/silviodelgado/maskerjs
*/
(function(e,t){"function"==typeof define&&define.amd?define([],t(e)):"object"==typeof exports?module.exports=t(e):e.Masker=t(e)})("undefined"!=typeof global?global:this.window||this.global,function(e){"use strict";String.prototype.replaceAll=function(e,t){let a=this;for(;a.indexOf(e)>=0;)a=a.replace(e,t);return a};const t={cpf:function(e){return e.value.replace(/\D/g,"").replace(/(\d{3})(\d)/,"$1.$2").replace(/(\d{3})(\d)/,"$1.$2").replace(/(\d{3})(\d{1,2})/,"$1-$2").replace(/(-\d{2})\d+?$/,"$1")},cnpj:function(e){return e.value.replace(/\D/g,"").replace(/(\d{2})(\d)/,"$1.$2").replace(/(\d{3})(\d)/,"$1.$2").replace(/(\d{3})(\d{1,4})/,"$1/$2").replace(/(\/\d{4})(\d{1,2})/,"$1-$2").replace(/(-\d{2})\d+?$/,"$1")},cpf_cnpj:function(e){let a=e.value==e.value.replace(/\D/g,"");return!a&&e.value.length<=14||a&&11==e.value.length?t.cpf(e):t.cnpj(e)},cep:function(e){return e.value.replace(/\D/g,"").replace(/(\d{5})(\d)/,"$1-$2").replace(/(-\d{3})\d+?$/,"$1")},phone:function(e){return e.value.replace(/\D/g,"").replace(/(\d{1,2})/,"($1").replace(/\((\d{2})(\d{1})/,"($1) $2").replace(/(\d{4})(\d{1,4})/,"$1-$2").replace(/(\d{4})-(\d)(\d{4})/,"$1$2-$3").replace(/(-\d{4})\d+?$/,"$1")},date:function(e){return e.value.replace(/\D/g,"").replace(/(\d{2})(\d)/,"$1/$2").replace(/(\d{2})(\d{1,4})/,"$1/$2").replace(/(\/\d{4})\d+?$/,"$1")},datetime:function(e){return e.value.replace(/\D/g,"").replace(/(\d{2})(\d)/,"$1/$2").replace(/(\d{2})(\d{1,4})/,"$1/$2").replace(/(\d{4})(\d{1,2})/,"$1 $2").replace(/(\d{4}) (\d{2})(\d{1,2})/,"$1 $2:$3").replace(/(\:\d{2})\d+?$/,"$1")},year_month:function(e){return e.value.length>7?e.value.substring(0,7):e.value.replace(/\D/g,"").replace(/(\d{4})(\d)/,"$1/$2").replace(/(\/\d{2})\d+?$/,"$1")},month_year:function(e){return e.value.length>7?e.value.substring(0,7):e.value.replace(/\D/g,"").replace(/(\d{2})(\d)/,"$1/$2").replace(/\/(\d{4})\d+?$/,"$1")},time:function(e){return e.value.replace(/\D/g,"").replace(/(\d{2})(\d)/,"$1:$2").replace(/(\:\d{2})\d+?$/,"$1")},money:function(e){let t=e.dataset.culture||"en-us",a=!e.dataset.masked;e.setAttribute("maxlength",18);let r=e.value;for(;r.length>0&&("0"==r.substring(0,1)||"."==r.substring(0,1)||","==r.substring(0,1));)r=r.substring(1);switch(1==r.length?r="0.0"+r:2==r.length&&(r="0."+r),t.toLowerCase()){case"pt-br":e.setAttribute("placeholder","0,00"),r=(a?parseFloat(r).toFixed(2).toString():r.replaceAll(".","").replace(",",".")).replace(/\D/g,"").replace(/(\d{1, 2})$/,"$1").replace(/(\d+)(\d{2})/,"$1,$2").replace(/(\d+)(\d{3})/,"$1.$2").replace(/(\d+)(\d{3})/,"$1.$2").replace(/(\d+)(\d{3})/,"$1.$2").replace(/(\d+)(\d{3})/,"$1.$2");break;default:e.setAttribute("placeholder","0.00"),r=(a?parseFloat(r).toFixed(2).toString():r.replace(",",".")).replace(/\D/g,"").replace(/(\d{1, 2})$/,"$1").replace(/(\d+)(\d{2})/,"$1.$2").replace(/(\d+)(\d{3})/,"$1,$2").replace(/(\d+)(\d{3})/,"$1,$2").replace(/(\d+)(\d{3})/,"$1,$2").replace(/(\d+)(\d{3})/,"$1,$2")}return e.dataset.masked=!0,r},ccard:function(e){let t=e.value.replace(/\D/g,""),a=t.length>0?t.substring(0,1):"";if(""==a)return"";switch(a){case"4":case"5":case"6":return e.setAttribute("maxlength",19),t.replace(/(\d{4})(\d)/,"$1 $2").replace(/( \d{4})(\d)/,"$1 $2").replace(/( \d{4})(\d)/,"$1 $2").replace(/( \d{4})(\d+?$)/,"$1 $2");case"3":let r=t.length>1?t.substring(1,1):"";if(""==r)return t;switch(r){case"6":case"8":return e.setAttribute("maxlength",16),t.replace(/(\d{4})(\d)/,"$1 $2").replace(/( \d{6})(\d)/,"$1 $2").replace(/( \d{4})(\d+?$)/,"$1 $2");case"4":case"7":return e.setAttribute("maxlength",17),t.replace(/(\d{4})(\d)/,"$1 $2").replace(/( \d{6})(\d)/,"$1 $2").replace(/( \d{5})(\d+?$)/,"$1 $2");default:return t}default:return t}},number:function(e){return e.value.replace(/\D/g,"")},float:function(e){return e.value.replace(/[^\-0-9.,]/g,"")},percent:function(e){let t=e.dataset.culture||"en-us",a=parseInt(e.dataset.precision||"2"),r=!e.dataset.masked,l=parseFloat(e.dataset.max||"100.00");e.setAttribute("maxlength",4+a);let d=e.value;for(;d.length>0&&("0"==d.substring(0,1)||"."==d.substring(0,1)||","==d.substring(0,1));)d=d.substring(1);for(;d.length>0&&d.length<=a;)d="0."+d.padStart(a,"0");let c=new RegExp("(\\d{1,"+a+"})$"),n=new RegExp("(\\d{1,3})(\\d{"+a+"})");switch(t.toLowerCase()){case"pt-br":e.setAttribute("placeholder","0,"+"0".padEnd(a,"0")),d=(r?parseFloat(d).toFixed(a).toString():d.replace(",",".")).replace(/\D/g,"").replace(c,"$1").replace(n,"$1,$2");break;default:e.setAttribute("placeholder","0."+"0".padEnd(a,"0")),d=(r?parseFloat(d).toFixed(a).toString():d).replace(/\D/g,"").replace(c,"$1").replace(n,"$1.$2")}e.dataset.masked=!0;let p=parseFloat(d.replace(/\D/g,"").replace(c,"$1").replace(n,"$1.$2"));return p>l&&(d=d.substring(0,d.length-1)),d}},a=function(){document.querySelectorAll("input[data-mask]").forEach(e=>{if(!e.dataset.mask)return!1;e.value=t[e.dataset.mask]?t[e.dataset.mask](e):e.value,e.addEventListener("input",a=>{null!=a.data||""==a.target.value?a.target.value=t[e.dataset.mask](a.target):a.target.value=a.target.value.substring(0,a.target.value.length)}),e.addEventListener("blur",a=>{a.target.value=t[e.dataset.mask](a.target)}),e.addEventListener("keyup",a=>{a.target.value=t[e.dataset.mask](a.target)}),e.addEventListener("focus",t=>{e.select()})})};return{init:a}});