I created and use this solution years ago in jQuery for the Plugin itself Maskedinput of Digitalbush:
$(".hibridCpf").on('focusin',function(){
var target = $(this);
var val = target.val();
target.unmask();
val = val.split(".").join("");
val = val.split("-").join("");
val = val.split("/").join("");
target.val(val);
});
$(".hibridCpf").on('focusout',function(){
var target = $(this);
var val = target.val();
val = val.split(".").join("");
val = val.split("-").join("");
val = val.split("/").join("");
if (val.length==11) {
target.mask("999.999.999-99");
target.val(val);
} else {
if (val.length==14) {
target.mask("99.999.999/9999-99");
target.val(val);
} else {
target.val('');
}
}
});
Test this example on jsfiddle.
It is obvious that the solution put forward by @rray is far simpler and recommends, but there are those cases where it is not possible to replace the application’s Mask Plugin for N reasons, then the way and improvise like I did kkkk...
This server solution also for zip code fields where the user can report a Brazilian or American zip code, using a similar logic...
PS: A mask plugin that I recommend and started using is the Mask Plugin created by the Brazilian @igorescobar (Github), he gathered the Mask Input and Mask Money in a single with more customization options, worth checking out!
With another plugin I know q da p do, could put the format of Cpf and cnpj.
– rray
So what I like about this is that it looks perfect, because when you click on the field it appears, unlike others that will appear as you type. I do not know if I put another plugin will conflict with this plugin that is already being used in other fields.
– Mauricio Ferraz
@Bacco I could not make it work with the answer that you mentioned being duplicated, it has no reference to the plugin that is being used.
– Mauricio Ferraz