Skip to content

Commit b82642d

Browse files
committed
Implemented addAsyncValidator victorjonsson#609
1 parent e9d2a9e commit b82642d

37 files changed

+6715
-314
lines changed

form-validator/brazil.js

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,138 @@
1-
/** File generated by Grunt -- do not modify
2-
* JQUERY-FORM-VALIDATOR
1+
(function (root, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
// AMD. Register as an anonymous module unless amdModuleId is set
4+
define(["jquery"], function (a0) {
5+
return (factory(a0));
6+
});
7+
} else if (typeof module === 'object' && module.exports) {
8+
// Node. Does not work with strict CommonJS, but
9+
// only CommonJS-like environments that support module.exports,
10+
// like Node.
11+
module.exports = factory(require("jquery"));
12+
} else {
13+
factory(root["jQuery"]);
14+
}
15+
}(this, function (jQuery) {
16+
17+
/**
18+
* jQuery Form Validator Module: Brazil
19+
* ------------------------------------------
20+
* Created by Eduardo Cuducos <http://cuducos.me/>
321
*
4-
* @version 2.3.68
5-
* @website http://formvalidator.net/
6-
* @author Victor Jonsson, http://victorjonsson.se
7-
* @license MIT
22+
* This form validation module adds validators typically used on
23+
* websites in the Brazil. This module adds the following validators:
24+
* - cpf
25+
* - cep
26+
* - brphone
27+
*
28+
* @website http://formvalidator.net/#brazil-validators
29+
* @license MIT
830
*/
9-
!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b(a)}):"object"==typeof module&&module.exports?module.exports=b(require("jquery")):b(a.jQuery)}(this,function(a){!function(a){a.formUtils.registerLoadedModule("brazil"),a.formUtils.addValidator({name:"cpf",validatorFunction:function(a){var b=a.replace(/\D/g,""),c=0,d=0,e=0,f=0;if(11!==b.length||"00000000000"===b)return!1;for(i=1;i<=9;i++)c+=parseInt(b.substring(i-1,i))*(11-i);if(e=10*c%11,e>=10&&(e=0),e!==parseInt(b.substring(9,10)))return!1;for(i=1;i<=10;i++)d+=parseInt(b.substring(i-1,i))*(12-i);return f=10*d%11,f>=10&&(f=0),f===parseInt(b.substring(10,11))},errorMessage:"",errorMessageKey:"badBrazilCPFAnswer"}),a.formUtils.addValidator({name:"brphone",validatorFunction:function(a){return!!a.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)},errorMessage:"",errorMessageKey:"badBrazilTelephoneAnswer"}),a.formUtils.addValidator({name:"cep",validatorFunction:function(a){return!!a.match(/^(\d){5}([-. ]){0,1}(\d){3}$/g)},errorMessage:"",errorMessageKey:"badBrazilCEPAnswer"})}(a)});
31+
(function($) {
32+
33+
$.formUtils.registerLoadedModule('brazil');
34+
35+
$.formUtils.addValidator({
36+
name : 'cpf',
37+
validatorFunction : function(string) {
38+
39+
// Based on this post from DevMedia:
40+
// http://www.devmedia.com.br/validar-cpf-com-javascript/23916
41+
42+
// clean up the input (digits only) and set some support vars
43+
var cpf = string.replace(/\D/g,'');
44+
var sum1 = 0;
45+
var sum2 = 0;
46+
var remainder1 = 0;
47+
var remainder2 = 0;
48+
49+
// skip special cases
50+
if (cpf.length !== 11 || cpf === '00000000000') {
51+
return false;
52+
}
53+
54+
// check 1st verification digit
55+
for (i = 1; i<= 9; i++) {
56+
sum1 += parseInt(cpf.substring(i - 1, i)) * (11 - i);
57+
}
58+
remainder1 = (sum1 * 10) % 11;
59+
if (remainder1 >= 10) {
60+
remainder1 = 0;
61+
}
62+
if (remainder1 !== parseInt(cpf.substring(9, 10))) {
63+
return false;
64+
}
65+
66+
// check 2nd verification digit
67+
for (i = 1; i <= 10; i++) {
68+
sum2 += parseInt(cpf.substring(i - 1, i)) * (12 - i);
69+
}
70+
remainder2 = (sum2 * 10) % 11;
71+
if (remainder2 >= 10) {
72+
remainder2 = 0;
73+
}
74+
if (remainder2 !== parseInt(cpf.substring(10, 11))) {
75+
return false;
76+
}
77+
78+
return true;
79+
80+
},
81+
errorMessage : '',
82+
errorMessageKey: 'badBrazilCPFAnswer'
83+
84+
});
85+
86+
$.formUtils.addValidator({
87+
name : 'brphone',
88+
validatorFunction : function(string) {
89+
90+
// validates telefones such as (having X as numbers):
91+
// (XX) XXXX-XXXX
92+
// (XX) XXXXX-XXXX
93+
// XX XXXXXXXX
94+
// XX XXXXXXXXX
95+
// XXXXXXXXXX
96+
// XXXXXXXXXXX
97+
// +XX XX XXXXX-XXXX
98+
// +X XX XXXX-XXXX
99+
// And so on…
100+
101+
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)) {
102+
return true;
103+
}
104+
105+
return false;
106+
107+
},
108+
errorMessage : '',
109+
errorMessageKey: 'badBrazilTelephoneAnswer'
110+
111+
});
112+
113+
$.formUtils.addValidator({
114+
name : 'cep',
115+
validatorFunction : function(string) {
116+
117+
// validates CEP such as (having X as numbers):
118+
// XXXXX-XXX
119+
// XXXXX.XXX
120+
// XXXXX XXX
121+
// XXXXXXXX
122+
123+
if (string.match(/^(\d){5}([-. ]){0,1}(\d){3}$/g)) {
124+
return true;
125+
}
126+
127+
return false;
128+
129+
},
130+
errorMessage : '',
131+
errorMessageKey: 'badBrazilCEPAnswer'
132+
133+
});
134+
135+
})(jQuery);
136+
137+
138+
}));

form-validator/date.js

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,103 @@
1-
/** File generated by Grunt -- do not modify
2-
* JQUERY-FORM-VALIDATOR
1+
(function (root, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
// AMD. Register as an anonymous module unless amdModuleId is set
4+
define(["jquery"], function (a0) {
5+
return (factory(a0));
6+
});
7+
} else if (typeof module === 'object' && module.exports) {
8+
// Node. Does not work with strict CommonJS, but
9+
// only CommonJS-like environments that support module.exports,
10+
// like Node.
11+
module.exports = factory(require("jquery"));
12+
} else {
13+
factory(root["jQuery"]);
14+
}
15+
}(this, function (jQuery) {
16+
17+
/**
18+
* jQuery Form Validator Module: Date
19+
* ------------------------------------------
20+
* Created by Victor Jonsson <http://www.victorjonsson.se>
21+
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
322
*
4-
* @version 2.3.68
5-
* @website http://formvalidator.net/
6-
* @author Victor Jonsson, http://victorjonsson.se
7-
* @license MIT
23+
* The following validators will be added by this module:
24+
* - Time (HH:mmm)
25+
* - Birth date
26+
*
27+
* @website http://formvalidator.net/#location-validators
28+
* @license MIT
829
*/
9-
!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b(a)}):"object"==typeof module&&module.exports?module.exports=b(require("jquery")):b(a.jQuery)}(this,function(a){!function(a){function b(a,b,c){var d=new Date,e=new Date;return d.setYear(a),d.setMonth(b),d.setDate(c),new Date(e.getTime()-d.getTime()).getUTCFullYear()-1970}a.formUtils.registerLoadedModule("date"),a.formUtils.addValidator({name:"time",validatorFunction:function(a){if(null===a.match(/^(\d{2}):(\d{2})$/))return!1;var b=parseInt(a.split(":")[0],10),c=parseInt(a.split(":")[1],10);return!(b>23||c>59)},errorMessage:"",errorMessageKey:"badTime"}),a.formUtils.addValidator({name:"birthdate",validatorFunction:function(c,d,e){var f="yyyy-mm-dd";d.valAttr("format")?f=d.valAttr("format"):"undefined"!=typeof e.dateFormat&&(f=e.dateFormat);var g=a.formUtils.parseDate(c,f);if(!g)return!1;var h=g[0],i=g[1],j=g[2],k=b(h,i,j),l=(d.valAttr("age-range")||"0-124").split("-");if(2!==l.length||!a.isNumeric(l[0])||!a.isNumeric(l[1]))throw new Error("Date range format invalid");return k>=l[0]&&k<=l[1]},errorMessage:"",errorMessageKey:"badDate"})}(a)});
30+
(function ($) {
31+
32+
$.formUtils.registerLoadedModule('date');
33+
34+
/*
35+
* Validate time hh:mm
36+
*/
37+
$.formUtils.addValidator({
38+
name: 'time',
39+
validatorFunction: function (time) {
40+
if (time.match(/^(\d{2}):(\d{2})$/) === null) {
41+
return false;
42+
} else {
43+
var hours = parseInt(time.split(':')[0], 10);
44+
var minutes = parseInt(time.split(':')[1], 10);
45+
if (hours > 23 || minutes > 59) {
46+
return false;
47+
}
48+
}
49+
return true;
50+
},
51+
errorMessage: '',
52+
errorMessageKey: 'badTime'
53+
});
54+
55+
/*
56+
* Is this a valid birth date
57+
*/
58+
$.formUtils.addValidator({
59+
name: 'birthdate',
60+
validatorFunction: function (val, $el, conf) {
61+
var dateFormat = 'yyyy-mm-dd';
62+
if ($el.valAttr('format')) {
63+
dateFormat = $el.valAttr('format');
64+
}
65+
else if (typeof conf.dateFormat !== 'undefined') {
66+
dateFormat = conf.dateFormat;
67+
}
68+
69+
var inputDate = $.formUtils.parseDate(val, dateFormat);
70+
if (!inputDate) {
71+
return false;
72+
}
73+
74+
var year = inputDate[0],
75+
month = inputDate[1],
76+
day = inputDate[2],
77+
age = getAge(year, month, day),
78+
allowedAgeRange = ($el.valAttr('age-range') || '0-124').split('-');
79+
80+
if (allowedAgeRange.length !== 2 || !$.isNumeric(allowedAgeRange[0]) || !$.isNumeric(allowedAgeRange[1])) {
81+
throw new Error('Date range format invalid');
82+
}
83+
84+
return age >= allowedAgeRange[0] && age <= allowedAgeRange[1];
85+
},
86+
errorMessage: '',
87+
errorMessageKey: 'badDate'
88+
});
89+
90+
91+
function getAge(otherDateYear, otherDateMonth, otherDateDay) {
92+
var otherDate = new Date(),
93+
nowDate = new Date();
94+
otherDate.setYear(otherDateYear);
95+
otherDate.setMonth(otherDateMonth);
96+
otherDate.setDate(otherDateDay);
97+
return new Date(nowDate.getTime() - otherDate.getTime()).getUTCFullYear() - 1970;
98+
}
99+
100+
})(jQuery);
101+
102+
103+
}));

0 commit comments

Comments
 (0)