Skip to content

Commit e1f74ac

Browse files
committed
Add Brazilian Validation Module
1 parent 73c8b60 commit e1f74ac

File tree

4 files changed

+147
-5
lines changed

4 files changed

+147
-5
lines changed

form-validator/brazil.dev.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* jQuery Form Validator Module: Brazil
3+
* ------------------------------------------
4+
* Created by Eduardo Cuducos <http://cuducos.me/>
5+
*
6+
* This form validation module adds validators typically used on
7+
* websites in the Brazil. This module adds the following validators:
8+
* - cpf
9+
* - cep
10+
* - brphone
11+
*
12+
* @website http://formvalidator.net/#brazil-validators
13+
* @license Dual licensed under the MIT or GPL Version 2 licenses
14+
* @version 2.2.52
15+
*/
16+
17+
$.formUtils.addValidator({
18+
name : 'cpf',
19+
validatorFunction : function(string) {
20+
21+
// Based on this post from DevMedia:
22+
// http://www.devmedia.com.br/validar-cpf-com-javascript/23916
23+
24+
// clean up the input (digits only) and set some support vars
25+
var cpf = string.replace(/\D/g,"");
26+
var sum1 = 0;
27+
var sum2 = 0;
28+
var remainder1 = 0;
29+
var remainder2 = 0;
30+
31+
// skip special cases
32+
if (cpf.length != 11 || cpf == "00000000000") {
33+
return false;
34+
}
35+
36+
// check 1st verification digit
37+
for (i=1; i<=9; i++) {
38+
sum1 += parseInt(cpf.substring(i - 1, i)) * (11 - i);
39+
}
40+
remainder1 = (sum * 10) % 11;
41+
if (remainder1 >= 10) {
42+
remainder1 = 0;
43+
}
44+
if (remainder != parseInt(cpf.substring(9, 10))) {
45+
return false;
46+
}
47+
48+
// check 2nd verification digit
49+
for (i = 1; i <= 10; i++) {
50+
sum2 += parseInt(strCPF.substring(i - 1, i)) * (12 - i);
51+
}
52+
remainder2 = (sum2 * 10) % 11;
53+
if (remainder2 >= 10) {
54+
remainder2 = 0;
55+
}
56+
if (remainder2 != parseInt(strCPF.substring(10, 11))) {
57+
return false;
58+
}
59+
60+
return true;
61+
62+
},
63+
errorMessage : '',
64+
errorMessageKey: 'badBrazilCPFAnswer'
65+
66+
});
67+
68+
$.formUtils.addValidator({
69+
name : 'brphone',
70+
validatorFunction : function(string) {
71+
72+
// validates telefones such as (having X as numbers):
73+
// (XX) XXXX-XXXX
74+
// (XX) XXXXX-XXXX
75+
// XX XXXXXXXX
76+
// XX XXXXXXXXX
77+
// XXXXXXXXXX
78+
// XXXXXXXXXXX
79+
// +XX XX XXXXX-XXXX
80+
// +X XX XXXX-XXXX
81+
// And so on…
82+
83+
if (string.match(/^(\+[\d]{1,3}[\s]{0,1}){0,1}(\(){0,1}(\d){2}(\)){0,1}(\s){0,1}(\d){4,5}([-. ]){0,1}(\d){4}$/g)) {
84+
return true;
85+
}
86+
87+
return false;
88+
89+
},
90+
errorMessage : '',
91+
errorMessageKey: 'badBrazilTelephoneAnswer'
92+
93+
});
94+
95+
$.formUtils.addValidator({
96+
name : 'cep',
97+
validatorFunction : function(string) {
98+
99+
// validates CEP such as (having X as numbers):
100+
// XXXXX-XXX
101+
// XXXXX.XXX
102+
// XXXXX XXX
103+
// XXXXXXXX
104+
105+
if (string.match(/^(\d){5}([-. ]){0,1}(\d){3}$/g)) {
106+
return true;
107+
}
108+
109+
return false;
110+
111+
},
112+
errorMessage : '',
113+
errorMessageKey: 'badBrazilCEPAnswer'
114+
115+
});

form-validator/form-test.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ <h2>HTML5 attributes</h2>
321321
<input type="reset" class="button">
322322
</p>
323323
</form>
324+
<hr />
325+
<form id="form-e">
326+
<h2>Validation Module: Brazil</h2>
327+
<div class="form-group">
328+
<label class="control-label">Telephone</label>
329+
<input name="brazil_telephone" data-validation="brphone" type="text" />
330+
</div>
331+
<div class="form-group">
332+
<label class="control-label">CEP</label>
333+
<input name="brazil_cep" data-validation="cep" type="text" />
334+
</div>
335+
<div class="form-group">
336+
<label class="control-label">CPF</label>
337+
<input name="brazil_cpf" data-validation="cpf" type="text" />
338+
</div>
339+
<p>
340+
<input type="submit" class="button">
341+
<input type="reset" class="button">
342+
</p>
343+
</form>
324344
</div>
325345
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
326346
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
@@ -361,7 +381,7 @@ <h2>HTML5 attributes</h2>
361381
lang : 'sv',
362382
sanitizeAll : 'trim', // only used on form C
363383
// borderColorOnError : 'purple',
364-
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', uk'+dev +( xtraModule ? ','+xtraModule:''),
384+
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', uk'+dev+' , brazil'+dev +( xtraModule ? ','+xtraModule:''),
365385
onModulesLoaded: function() {
366386
$('#country-suggestions').suggestCountry();
367387
$('#swedish-county-suggestions').suggestSwedishCounty();
@@ -395,6 +415,7 @@ <h2>HTML5 attributes</h2>
395415
window.applyValidation(false, '#form-b', 'element');
396416
window.applyValidation(true, '#form-c', $('#error-container'), 'sanitize'+dev);
397417
window.applyValidation(true, '#form-d', 'element', 'html5'+dev);
418+
window.applyValidation(true, '#form-e');
398419

399420
// Load one module outside $.validate() even though you do not have to
400421
$.formUtils.loadModules('date'+dev+'.js', false, false);

form-validator/jquery.form-validator.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @website http://formvalidator.net/
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 2.2.51
8+
* @version 2.2.52
99
*/
1010
(function ($) {
1111

@@ -1415,7 +1415,10 @@
14151415
imageTooSmall : 'the image was too small',
14161416
min : 'min',
14171417
max : 'max',
1418-
imageRatioNotAccepted : 'Image ratio is not be accepted'
1418+
imageRatioNotAccepted : 'Image ratio is not be accepted',
1419+
badBrazilTelephoneAnswer: 'The phone number entered is invalid',
1420+
badBrazilCEPAnswer: 'The CEP entered is invalid',
1421+
badBrazilCPFAnswer: 'The CPF entered is invalid'
14191422
}
14201423
};
14211424

form-validator/lang/pt.dev.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.51
9+
* @version 2.2.52
1010
*/
1111
(function($, window) {
1212

@@ -53,7 +53,10 @@
5353
imageTooSmall: 'a imagem é muito pequena',
5454
min: 'min',
5555
max: 'max',
56-
imageRatioNotAccepted : 'A proporção da imagem (largura x altura) não é válida'
56+
imageRatioNotAccepted : 'A proporção da imagem (largura x altura) não é válida',
57+
badBrazilTelephoneAnswer: 'O número de telefone digitado é inválido',
58+
badBrazilCEPAnswer: 'O CEP digitado é inválido',
59+
badBrazilCPFAnswer: 'O CPF digitado é inválido'
5760
};
5861

5962
});

0 commit comments

Comments
 (0)