Sha256: 29f1273ed931a4b0174aed7c958e44b9ed1289a556b10ac71d059127fcadac19

Contents?: true

Size: 1.31 KB

Versions: 33

Compression:

Stored size: 1.31 KB

Contents

var composed, cpfable, stampit, validations;

validations = require('../validatable');

stampit = require('../../../vendor/stampit');

cpfable = stampit({
  validate_format: function(value) {
    var c, d1, dv, i, v, _i, _j;

    value = value.replace(/[\.\-]/g, "");
    if (value.length < 11) {
      return false;
    }
    if (value.match(/^(0+|1+|2+|3+|4+|5+|6+|7+|8+|9+)$/)) {
      return false;
    }
    c = value.substr(0, 9);
    dv = value.substr(9, 2);
    d1 = 0;
    v = false;
    i = 0;
    for (i = _i = 1; _i <= 9; i = ++_i) {
      d1 += c.charAt(i) * (10 - i);
    }
    if (d1 === 0) {
      return false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) {
      d1 = 0;
    }
    if (+dv.charAt(0 !== d1)) {
      return false;
    }
    d1 *= 2;
    for (i = _j = 1; _j <= 9; i = ++_j) {
      d1 += c.charAt(i) * (11 - i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) {
      d1 = 0;
    }
    if (+dv.charAt(1) !== d1) {
      return false;
    }
    return true;
  },
  validate_each: function(record, attribute, value) {
    if (value && !this.validate_format(value)) {
      return record.errors.add(attribute, 'cpf', this.options);
    }
  }
});

composed = stampit.compose(validations.validatable, cpfable);

composed.definition_key = 'validates_cpf_format';

validations.manager.validators.cpf = composed;

Version data entries

33 entries across 20 versions & 1 rubygems

Version Path
ende-0.2.9 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.8 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.7 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.6 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.5 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.4 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.3 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.3 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.2 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.2 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.1 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.1 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.0 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.2.0 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.14 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.14 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.13 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.13 vendor/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.12 vendor/assets/components/indefinido-indemma/lib/record/validations/cpf.js
ende-0.1.12 vendor/components/indefinido-indemma/lib/record/validations/cpf.js