Skip to content

Commit

Permalink
Tratamento de máscara adicionado
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgomess committed Oct 10, 2014
1 parent 4470eab commit aa53d53
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
55 changes: 41 additions & 14 deletions jquery.cpfcnpj.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
/*!
* jQuery CPF/CNPJ Validator Plugin v1.0.0
* jQuery CPF/CNPJ Validator Plugin v1.1.0
* Developed by: Guilherme Gomes ([email protected])
* Date: 2014-10-03
* Date: 2014-10-06
*/
(function ($) {
var type = null;

$.fn.cpfcnpj = function (options) {
// Default settings
var settings = $.extend({
mask: true,
validate: 'cpfcnpj',
event: 'focusout',
handler: $(this),
ifValid: null,
ifInvalid: null
}, options);

if (settings.mask) {
if (jQuery().mask == null) {
settings.mask = false;
console.log("jQuery mask not found.");
}
else {
if (settings.validate == 'cpf') {
$(this).mask('000.000.000-00');
}
else if (settings.validate == 'cnpj') {
$(this).mask('00.000.000/0000-00');
}
else {
var ctrl = $(this);
var opt = {
onKeyPress: function (field) {
var masks = ['000.000.000-009', '00.000.000/0000-00'];
msk = (field.length > 14) ? masks[1] : masks[0];
ctrl.mask(msk, this);
}
};

$(this).mask('000.000.000-009', opt);
}
}

}

return this.each(function () {
var valid = null;
var control = $(this);

$(document).on(settings.event, settings.handler,
function () {
if (control.val().length == 14 || control.val().length == 18) {
Expand All @@ -44,18 +73,16 @@
else valid = false;

if ($.isFunction(settings.ifValid)) {
if(valid != null && valid){
if($.isFunction(settings.ifValid))
{
if (valid != null && valid) {
if ($.isFunction(settings.ifValid)) {
type = settings.validate == 'cpfcnpj' ? type : settings.validate;

var callbacks = $.Callbacks();
callbacks.add(settings.ifValid);
callbacks.add(settings.ifValid);
callbacks.fire(type);
}
}
else if($.isFunction(settings.ifInvalid))
{
else if ($.isFunction(settings.ifInvalid)) {
settings.ifInvalid.call(this);
}
}
Expand All @@ -66,11 +93,11 @@
function validate_cnpj(val) {

if (val.match(/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/) != null) {
var val1 = val.substring(0, 2);
var val2 = val.substring(3, 6);
var val3 = val.substring(7, 10);
var val4 = val.substring(11, 15);
var val5 = val.substring(16, 18);
var val1 = val.substring(0, 2);
var val2 = val.substring(3, 6);
var val3 = val.substring(7, 10);
var val4 = val.substring(11, 15);
var val5 = val.substring(16, 18);

var i;
var number;
Expand Down
4 changes: 2 additions & 2 deletions jquery.cpfcnpj.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa53d53

Please sign in to comment.