Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Created June 26, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovalney/3386906cca42e757504d66bd4a31f1c9 to your computer and use it in GitHub Desktop.
Save mariovalney/3386906cca42e757504d66bd4a31f1c9 to your computer and use it in GitHub Desktop.
Add the most used masks. Requires jQuery Mask Plugin.
'use strict';
jQuery(document).ready(function($) {
// Nineth Digit behavior
var ninethDigitMask = function( val ) {
return ( val.replace( /\D/g, '' ).length === 11 ) ? '(00) 00000-0000' : '(00) 0000-00009';
}
// Multiple Documents Behaviour
var multipleDocumentsMask = function( val, e, field, options ) {
return ( val.replace( /\D/g, '' ).length > 11 ) ? '00.000.000/0000-00' : '000.000.000-00999';
}
window.VizirMaskInputs = function() {
// Masks
$('.mask-date').mask( '00/00/0000' );
$('.mask-time').mask( '00:00:00' );
$('.mask-date_time').mask( '00/00/0000 00:00:00' );
$('.mask-mixed').mask( 'AAA 000-S0S' );
$('.mask-cep').mask( '00000-000' );
$('.mask-cpf').mask( '000.000.000-00', { reverse: true } );
$('.mask-cnpj').mask( '00.000.000/0000-00', { reverse: true } );
$('.mask-money').mask( '000.000.000.000.000,00', { reverse: true } );
$('.mask-money2').mask( 'R$ #.##0,00', { reverse: true } );
$('.mask-document').mask( multipleDocumentsMask, { onKeyPress: function(v, e, f, o) { f.mask( multipleDocumentsMask.apply( {}, arguments ), 0 ); } } );
$('.mask-phone').mask( ninethDigitMask, { onKeyPress: function(v, e, f, o) { f.mask( ninethDigitMask.apply( {}, arguments ), 0 ); } } );
$('.mask-number').keyup(function() {
this.value = this.value.replace(/[^0-9\.]/g, '');
});
}
VizirMaskInputs();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment