Skip to content

Instantly share code, notes, and snippets.

@brunofrank
Created August 4, 2017 12:23
Show Gist options
  • Save brunofrank/d5e9610c7f56e3f8be804d66e4b185b8 to your computer and use it in GitHub Desktop.
Save brunofrank/d5e9610c7f56e3f8be804d66e4b185b8 to your computer and use it in GitHub Desktop.
//= require jquery.maskMoney
//= require jquery.mask
var Mask = {
phoneMasks: ['(00) 00000-0000', '(00) 0000-00000'],
maskBehavior: function (val, e, field, options){
return val.length > 14 ? Mask.phoneMasks[0] : Mask.phoneMasks[1];
},
maskParents: function(mainEl){
$(mainEl).find('.currency-with-neg').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, allowNegative: true });
$(mainEl).find('.currency').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true});
$(mainEl).find('.stock').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, precision: 3});
$(mainEl).find('.stock-neg').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, precision: 3, allowNegative: true});
$(mainEl).find('.total-money').maskMoney({ symbol: "", decimal: ".", thousands: "", allowZero: true });
$(mainEl).find('.integer').mask('000000000000000');
$(mainEl).find('.cep').mask('#####-###');
$(mainEl).find('.phone').mask(Mask.maskBehavior, {onKeyPress:
function(val, e, field, options) {
field.mask(Mask.maskBehavior(val, e, field, options), options);
}
});
$(mainEl).find('.date').mask('##/##/####');
$(mainEl).find('.date-time').mask('##/##/#### ##:##');
$(mainEl).find('.time').mask('##:##');
$(mainEl).find('.cpf').mask('###.###.###-##');
$(mainEl).find('.cnpj').mask('##.###.###/####-##');
$(mainEl).find('.money').maskMoney({ prefix: "R$ ", decimal: ",", thousands: "", allowZero: true });
},
setMasks: function(mainEl){
$('.currency-with-neg').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, allowNegative: true });
$('.currency').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true});
$('.stock').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, precision: 3});
$('.stock-neg').maskMoney({ symbol: "", decimal: ",", thousands: "", allowZero: true, precision: 3, allowNegative: true});
$('.total-money').maskMoney({ symbol: "", decimal: ".", thousands: "", allowZero: true });
$('.integer').mask('000000000000000');
$('.cep').mask('#####-###');
$('.phone').mask(Mask.maskBehavior, {onKeyPress:
function(val, e, field, options) {
field.mask(Mask.maskBehavior(val, e, field, options), options);
}
});
$('.date').mask('##/##/####');
$('.date-time').mask('##/##/#### ##:##');
$('.time').mask('##:##');
$('.cpf').mask('###.###.###-##');
$('.cnpj').mask('##.###.###/####-##');
$('.money').maskMoney({ prefix: "R$ ", decimal: ",", thousands: "", allowZero: true });
}
}
$(document).on('ready turbolinks:load', Mask.setMasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment