dojox/validate/br.js

  • Provides:

    • dojox.validate.br
  • Requires:

    • dojox.validate._base in common
  • dojox.validate.br.isValidCnpj

    • type
      Function
    • parameters:
      • value: (typeof String)
        The CNPJ/CGC number in ##.###.###/####-##, ########/####-##,
        ############-## or ############## format
    • source: [view]
       if(!dojo.isString(value)){
        if(!value){
         return false;
        }
        value = value + "";
        while(value.length < 14){
         value = "0" + value;
        }
       }
       var flags = {
        format: [
         "##.###.###/####-##",
         "########/####-##",
         "############-##",
         "##############"
        ]
       };
       if(dojox.validate.isNumberFormat(value, flags)){
        // Matched the initial test, so break this down into the
        // parts to be validated.
        value = value.replace("/", "").replace(/\./g, "").replace("-", "");
        var cgc = [];
        var dv = [];
        var i, j, tmp;


        // Check for obvious bad combos
        // all 0s to all 9's.
        for(i = 0; i < 10; i++){
         tmp = "";
         for(j = 0; j < value.length; j++){
          tmp += "" + i;
         }
         if(value === tmp){
          return false;
         }
        }


        //Split out the DV from the main number.
        for(i = 0; i < 12; i++){
         cgc.push(parseInt(value.charAt(i), 10));
        }
        for(i = 12; i < 14; i++){
         dv.push(parseInt(value.charAt(i), 10));
        }

        
        var base = [9,8,7,6,5,4,3,2,9,8,7,6].reverse();
        var sum = 0;
        for(i = 0; i < cgc.length; i++){
         sum += cgc[i] * base[i];
        }
        var dv0 = sum % 11;
        if(dv0 == dv[0]){
         // Still seems valid, keep going.
         sum = 0;
         base = [9,8,7,6,5,4,3,2,9,8,7,6,5].reverse();
         cgc.push(dv0);
         for(i = 0; i < cgc.length; i++){
          sum += cgc[i] * base[i];
         }
         var dv1 = sum % 11;
         if(dv1 === dv[1]){
          // Whew, looks valid.
          return true;
         }
        }
       }
       return false;
    • summary
      Validates a CNPJ/CGC number
  • dojox.validate.br.computeCnpjDv

    • type
      Function
    • parameters:
      • value: (typeof String)
        The CGC number in ##.###.###/#### or ############ format
    • source: [view]
       if(!dojo.isString(value)){
        if(!value){
         return "";
        }
        value = value + "";
        while(value.length < 12){
         value = "0" + value;
        }
       }
       var flags = {
        format: [
         "##.###.###/####",
         "########/####",
         "############"
        ]
       };
       if(dojox.validate.isNumberFormat(value, flags)){
        // Matched the initial test, so break this down into the
        // parts to compute the DV.
        value = value.replace("/", "").replace(/\./g, "");
        var cgc = [];
        var i, j, tmp;


        // Check for obvious bad combos
        // all 0s to all 9's.
        for(i = 0; i < 10; i++){
         tmp = "";
         for(j = 0; j < value.length; j++){
          tmp += "" + i;
         }
         if(value === tmp){
          return "";
         }
        }


        for(i = 0; i < value.length; i++){
         cgc.push(parseInt(value.charAt(i), 10));
        }
        var base = [9,8,7,6,5,4,3,2,9,8,7,6].reverse();
        var sum = 0;
        for(i = 0; i < cgc.length; i++){
         sum += cgc[i] * base[i];
        }
        var dv0 = sum % 11;
        sum = 0;
        base = [9,8,7,6,5,4,3,2,9,8,7,6,5].reverse();
        cgc.push(dv0);
        for(i = 0; i < cgc.length; i++){
         sum += cgc[i] * base[i];
        }
        var dv1 = sum % 11;
        return ("" + dv0) + dv1;
       }
       return "";
    • summary
      Generate the DV code (checksum part) for a Cnpj number
  • dojox.validate.br.isValidCpf

    • type
      Function
    • parameters:
      • value: (typeof String)
        The CPF number in #########-## or ###########,
        format
    • source: [view]
       if(!dojo.isString(value)){
        if(!value){
         return false;
        }
        value = value + "";
        while(value.length < 11){
         value = "0" + value;
        }
       }
       var flags = {
        format: [
         "###.###.###-##",
         "#########-##",
         "###########"
        ]
       };
       if(dojox.validate.isNumberFormat(value, flags)){
        // Matched the initial test, so break this down into the
        // parts to be validated.
        value = value.replace("-", "").replace(/\./g, "");
        var cpf = [];
        var dv = [];
        var i, j, tmp;


        // Check for obvious bad combos
        // all 0s to all 9's.
        for(i = 0; i < 10; i++){
         tmp = "";
         for(j = 0; j < value.length; j++){
          tmp += "" + i;
         }
         if(value === tmp){
          return false;
         }
        }


        //Split out the DV from the main number.
        for(i = 0; i < 9; i++){
         cpf.push(parseInt(value.charAt(i), 10));
        }
        for(i = 9; i < 12; i++){
         dv.push(parseInt(value.charAt(i), 10));
        }

        
        var base = [9,8,7,6,5,4,3,2,1].reverse();
        var sum = 0;
        for(i = 0; i < cpf.length; i++){
         sum += cpf[i] * base[i];
        }
        var dv0 = sum % 11;
        if(dv0 == dv[0]){
         // Still seems valid, keep going.
         sum = 0;
         base = [9,8,7,6,5,4,3,2,1,0].reverse();
         cpf.push(dv0);
         for(i = 0; i < cpf.length; i++){
          sum += cpf[i] * base[i];
         }
         var dv1 = sum % 11;
         if(dv1 === dv[1]){
          // Whew, looks valid.
          return true;
         }
        }
       }
       return false;
    • summary
      Validates a CPF number
  • dojox.validate.br.computeCpfDv

    • type
      Function
    • parameters:
      • value: (typeof String)
        The CPF number in ######### format
    • source: [view]
       if(!dojo.isString(value)){
        if(!value){
         return "";
        }
        value = value + "";
        while(value.length < 9){
         value = "0" + value;
        }
       }
       var flags = {
        format: [
         "###.###.###",
         "#########"
        ]
       };
       if(dojox.validate.isNumberFormat(value, flags)){
        // Matched the initial test, so break this down into the
        // parts to compute the DV.
        value = value.replace(/\./g, "");
        var cpf = [];

        
        // Check for obvious bad combos
        // all 0s to all 9's.
        for(i = 0; i < 10; i++){
         tmp = "";
         for(j = 0; j < value.length; j++){
          tmp += "" + i;
         }
         if(value === tmp){
          return "";
         }
        }


        for(i = 0; i < value.length; i++){
         cpf.push(parseInt(value.charAt(i), 10));
        }
        var base = [9,8,7,6,5,4,3,2,1].reverse();
        var sum = 0;
        for(i = 0; i < cpf.length; i++){
         sum += cpf[i] * base[i];
        }
        var dv0 = sum % 11;
        sum = 0;
        base = [9,8,7,6,5,4,3,2,1,0].reverse();
        cpf.push(dv0);
        for(i = 0; i < cpf.length; i++){
         sum += cpf[i] * base[i];
        }
        var dv1 = sum % 11;
        return ("" + dv0) + dv1;
       }
       return "";
    • summary
      Generate the DV code (checksum part) for a CPF number
  • dojox.validate.br

    • type
      Object
    • summary
  • dojox.validate

    • type
      Object
    • summary
  • dojox

    • type
      Object
    • summary