root / trunk / web / dojo / dojox / validate / br.js @ 13
History | View | Annotate | Download (3.46 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.validate.br"]){ |
||
9 | dojo._hasResource["dojox.validate.br"]=true; |
||
10 | dojo.provide("dojox.validate.br");
|
||
11 | dojo.require("dojox.validate._base");
|
||
12 | dojox.validate.br.isValidCnpj=function(_1){ |
||
13 | if(!dojo.isString(_1)){
|
||
14 | if(!_1){
|
||
15 | return false; |
||
16 | } |
||
17 | _1=_1+"";
|
||
18 | while(_1.length<14){ |
||
19 | _1="0"+_1;
|
||
20 | } |
||
21 | } |
||
22 | var _2={format:["##.###.###/####-##","########/####-##","############-##","##############"]}; |
||
23 | if(dojox.validate.isNumberFormat(_1,_2)){
|
||
24 | _1=_1.replace("/","").replace(/\./g,"").replace("-",""); |
||
25 | var _3=[];
|
||
26 | var dv=[];
|
||
27 | var i,j,_4;
|
||
28 | for(i=0;i<10;i++){ |
||
29 | _4="";
|
||
30 | for(j=0;j<_1.length;j++){ |
||
31 | _4+=""+i;
|
||
32 | } |
||
33 | if(_1===_4){
|
||
34 | return false; |
||
35 | } |
||
36 | } |
||
37 | for(i=0;i<12;i++){ |
||
38 | _3.push(parseInt(_1.charAt(i),10));
|
||
39 | } |
||
40 | for(i=12;i<14;i++){ |
||
41 | dv.push(parseInt(_1.charAt(i),10));
|
||
42 | } |
||
43 | var _5=[9,8,7,6,5,4,3,2,9,8,7,6].reverse(); |
||
44 | var _6=0; |
||
45 | for(i=0;i<_3.length;i++){ |
||
46 | _6+=_3[i]*_5[i]; |
||
47 | } |
||
48 | var _7=_6%11; |
||
49 | if(_7==dv[0]){ |
||
50 | _6=0;
|
||
51 | _5=[9,8,7,6,5,4,3,2,9,8,7,6,5].reverse(); |
||
52 | _3.push(_7); |
||
53 | for(i=0;i<_3.length;i++){ |
||
54 | _6+=_3[i]*_5[i]; |
||
55 | } |
||
56 | var _8=_6%11; |
||
57 | if(_8===dv[1]){ |
||
58 | return true; |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | return false; |
||
63 | }; |
||
64 | dojox.validate.br.computeCnpjDv=function(_9){ |
||
65 | if(!dojo.isString(_9)){
|
||
66 | if(!_9){
|
||
67 | return ""; |
||
68 | } |
||
69 | _9=_9+"";
|
||
70 | while(_9.length<12){ |
||
71 | _9="0"+_9;
|
||
72 | } |
||
73 | } |
||
74 | var _a={format:["##.###.###/####","########/####","############"]}; |
||
75 | if(dojox.validate.isNumberFormat(_9,_a)){
|
||
76 | _9=_9.replace("/","").replace(/\./g,""); |
||
77 | var _b=[];
|
||
78 | var i,j,_c;
|
||
79 | for(i=0;i<10;i++){ |
||
80 | _c="";
|
||
81 | for(j=0;j<_9.length;j++){ |
||
82 | _c+=""+i;
|
||
83 | } |
||
84 | if(_9===_c){
|
||
85 | return ""; |
||
86 | } |
||
87 | } |
||
88 | for(i=0;i<_9.length;i++){ |
||
89 | _b.push(parseInt(_9.charAt(i),10));
|
||
90 | } |
||
91 | var _d=[9,8,7,6,5,4,3,2,9,8,7,6].reverse(); |
||
92 | var _e=0; |
||
93 | for(i=0;i<_b.length;i++){ |
||
94 | _e+=_b[i]*_d[i]; |
||
95 | } |
||
96 | var _f=_e%11; |
||
97 | _e=0;
|
||
98 | _d=[9,8,7,6,5,4,3,2,9,8,7,6,5].reverse(); |
||
99 | _b.push(_f); |
||
100 | for(i=0;i<_b.length;i++){ |
||
101 | _e+=_b[i]*_d[i]; |
||
102 | } |
||
103 | var dv1=_e%11; |
||
104 | return (""+_f)+dv1; |
||
105 | } |
||
106 | return ""; |
||
107 | }; |
||
108 | dojox.validate.br.isValidCpf=function(_10){ |
||
109 | if(!dojo.isString(_10)){
|
||
110 | if(!_10){
|
||
111 | return false; |
||
112 | } |
||
113 | _10=_10+"";
|
||
114 | while(_10.length<11){ |
||
115 | _10="0"+_10;
|
||
116 | } |
||
117 | } |
||
118 | var _11={format:["###.###.###-##","#########-##","###########"]}; |
||
119 | if(dojox.validate.isNumberFormat(_10,_11)){
|
||
120 | _10=_10.replace("-","").replace(/\./g,""); |
||
121 | var cpf=[];
|
||
122 | var dv=[];
|
||
123 | var i,j,tmp;
|
||
124 | for(i=0;i<10;i++){ |
||
125 | tmp="";
|
||
126 | for(j=0;j<_10.length;j++){ |
||
127 | tmp+=""+i;
|
||
128 | } |
||
129 | if(_10===tmp){
|
||
130 | return false; |
||
131 | } |
||
132 | } |
||
133 | for(i=0;i<9;i++){ |
||
134 | cpf.push(parseInt(_10.charAt(i),10));
|
||
135 | } |
||
136 | for(i=9;i<12;i++){ |
||
137 | dv.push(parseInt(_10.charAt(i),10));
|
||
138 | } |
||
139 | var _12=[9,8,7,6,5,4,3,2,1].reverse(); |
||
140 | var sum=0; |
||
141 | for(i=0;i<cpf.length;i++){ |
||
142 | sum+=cpf[i]*_12[i]; |
||
143 | } |
||
144 | var dv0=sum%11; |
||
145 | if(dv0==dv[0]){ |
||
146 | sum=0;
|
||
147 | _12=[9,8,7,6,5,4,3,2,1,0].reverse(); |
||
148 | cpf.push(dv0); |
||
149 | for(i=0;i<cpf.length;i++){ |
||
150 | sum+=cpf[i]*_12[i]; |
||
151 | } |
||
152 | var dv1=sum%11; |
||
153 | if(dv1===dv[1]){ |
||
154 | return true; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | return false; |
||
159 | }; |
||
160 | dojox.validate.br.computeCpfDv=function(_13){ |
||
161 | if(!dojo.isString(_13)){
|
||
162 | if(!_13){
|
||
163 | return ""; |
||
164 | } |
||
165 | _13=_13+"";
|
||
166 | while(_13.length<9){ |
||
167 | _13="0"+_13;
|
||
168 | } |
||
169 | } |
||
170 | var _14={format:["###.###.###","#########"]}; |
||
171 | if(dojox.validate.isNumberFormat(_13,_14)){
|
||
172 | _13=_13.replace(/\./g,""); |
||
173 | var cpf=[];
|
||
174 | for(i=0;i<10;i++){ |
||
175 | tmp="";
|
||
176 | for(j=0;j<_13.length;j++){ |
||
177 | tmp+=""+i;
|
||
178 | } |
||
179 | if(_13===tmp){
|
||
180 | return ""; |
||
181 | } |
||
182 | } |
||
183 | for(i=0;i<_13.length;i++){ |
||
184 | cpf.push(parseInt(_13.charAt(i),10));
|
||
185 | } |
||
186 | var _15=[9,8,7,6,5,4,3,2,1].reverse(); |
||
187 | var sum=0; |
||
188 | for(i=0;i<cpf.length;i++){ |
||
189 | sum+=cpf[i]*_15[i]; |
||
190 | } |
||
191 | var dv0=sum%11; |
||
192 | sum=0;
|
||
193 | _15=[9,8,7,6,5,4,3,2,1,0].reverse(); |
||
194 | cpf.push(dv0); |
||
195 | for(i=0;i<cpf.length;i++){ |
||
196 | sum+=cpf[i]*_15[i]; |
||
197 | } |
||
198 | var dv1=sum%11; |
||
199 | return (""+dv0)+dv1; |
||
200 | } |
||
201 | return ""; |
||
202 | }; |
||
203 | } |