Skip to content

Commit b7d6ffa

Browse files
committed
victorjonsson#479 support inline validation modules
1 parent 5158773 commit b7d6ffa

File tree

16 files changed

+439
-399
lines changed

16 files changed

+439
-399
lines changed

src/main/module-loader.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717
*/
1818
loadedModules: {},
1919

20+
/**
21+
* @param {String} name
22+
*/
23+
registerLoadedModule: function (name) {
24+
this.loadedModules[$.trim(name).toLowerCase()] = true;
25+
},
26+
27+
/**
28+
* @param {String} name
29+
* @return {Boolean}
30+
*/
31+
hasLoadedModule: function (name) {
32+
return $.trim(name).toLowerCase() in this.loadedModules;
33+
},
34+
2035
/**
2136
* @example
2237
* $.formUtils.loadModules('date, security.dev');
@@ -30,36 +45,32 @@
3045
* name ends with .dev
3146
*
3247
* @param {String} modules - Comma separated string with module file names (no directory nor file extension)
33-
* @param {String} [path] - Optional, path where the module files is located if their not in the same directory as the core modules
34-
* @param {function} [callback] - Optional, whether or not to fire event 'load' when modules finished loading
48+
* @param {String} [path] - Path where the module files are located if their not in the same directory as the core modules
49+
* @param {function} [callback] - Callback invoked when all modules are loaded
3550
*/
3651
loadModules: function (modules, path, callback) {
3752

3853
if ($.formUtils.isLoadingModules) {
3954
setTimeout(function () {
4055
$.formUtils.loadModules(modules, path, callback);
41-
}, 10);
56+
}, 100);
4257
return;
4358
}
4459

45-
var hasLoadedAnyModule = false,
46-
loadModuleScripts = function (modules, path) {
60+
var loadModuleScripts = function (modules, path) {
4761

4862
var moduleList = $.split(modules),
4963
numModules = moduleList.length,
5064
moduleLoadedCallback = function () {
5165
numModules--;
5266
if (numModules === 0) {
5367
$.formUtils.isLoadingModules = false;
54-
if (callback && hasLoadedAnyModule) {
55-
if( typeof callback === 'function' ) {
68+
if (typeof callback === 'function') {
5669
callback();
57-
}
5870
}
5971
}
6072
};
6173

62-
6374
if (numModules > 0) {
6475
$.formUtils.isLoadingModules = true;
6576
}
@@ -69,44 +80,33 @@
6980

7081
$.each(moduleList, function (i, modName) {
7182
modName = $.trim(modName);
72-
if (modName.length === 0) {
83+
if (modName.length === 0 || $.formUtils.hasLoadedModule(modName)) {
7384
moduleLoadedCallback();
74-
}
75-
else {
85+
} else {
7686
var scriptUrl = path + modName + (modName.slice(-3) === '.js' ? '' : '.js'),
7787
script = document.createElement('SCRIPT');
7888

79-
if (scriptUrl in $.formUtils.loadedModules) {
80-
// already loaded
81-
moduleLoadedCallback();
82-
}
83-
else {
84-
85-
// Remember that this script is loaded
86-
$.formUtils.loadedModules[scriptUrl] = 1;
87-
hasLoadedAnyModule = true;
88-
89-
if (typeof define === 'function' && define.amd) {
90-
require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback);
91-
} else {
92-
// Load the script
93-
script.type = 'text/javascript';
94-
script.onload = moduleLoadedCallback;
95-
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
96-
script.onerror = function() {
97-
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
98-
};
99-
script.onreadystatechange = function () {
100-
// IE 7 fix
101-
if (this.readyState === 'complete' || this.readyState === 'loaded') {
102-
moduleLoadedCallback();
103-
// Handle memory leak in IE
104-
this.onload = null;
105-
this.onreadystatechange = null;
106-
}
107-
};
108-
appendToElement.appendChild(script);
109-
}
89+
if (typeof define === 'function' && define.amd) {
90+
require([scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' )], moduleLoadedCallback);
91+
} else {
92+
// Load the script
93+
script.type = 'text/javascript';
94+
script.onload = moduleLoadedCallback;
95+
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
96+
script.onerror = function() {
97+
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
98+
moduleLoadedCallback();
99+
};
100+
script.onreadystatechange = function () {
101+
// IE 7 fix
102+
if (this.readyState === 'complete' || this.readyState === 'loaded') {
103+
moduleLoadedCallback();
104+
// Handle memory leak in IE
105+
this.onload = null;
106+
this.onreadystatechange = null;
107+
}
108+
};
109+
appendToElement.appendChild(script);
110110
}
111111
}
112112
});
@@ -136,7 +136,15 @@
136136
};
137137

138138
if (!findScriptPathAndLoadModules()) {
139-
$(findScriptPathAndLoadModules);
139+
$(function () {
140+
var hasLoadedModuleScripts = findScriptPathAndLoadModules();
141+
if (!hasLoadedModuleScripts) {
142+
// The modules may have been inserted via a minified script
143+
if (typeof callback === 'function') {
144+
callback();
145+
}
146+
}
147+
});
140148
}
141149
}
142150
}

src/main/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
});
163163

164164
if (conf.modules !== '') {
165-
$.formUtils.loadModules(conf.modules, false, function() {
165+
$.formUtils.loadModules(conf.modules, null, function() {
166166
if (typeof conf.onModulesLoaded === 'function') {
167167
conf.onModulesLoaded();
168168
}

src/modules/brazil.js

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

src/modules/date.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
(function($) {
1515

16+
$.formUtils.registerLoadedModule('date');
17+
1618
/*
1719
* Validate time hh:mm
1820
*/

src/modules/file.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
(function($, window) {
1717

18-
'use strict';
18+
$.formUtils.registerLoadedModule('file');
19+
20+
'use strict';
1921

2022
var SUPPORTS_FILE_READER = typeof window.FileReader !== 'undefined',
2123

src/modules/html5.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
'use strict';
2424

25+
$.formUtils.registerLoadedModule('html5');
26+
2527
var SUPPORTS_PLACEHOLDER = 'placeholder' in document.createElement('INPUT'),
2628
SUPPORTS_DATALIST = 'options' in document.createElement('DATALIST'),
2729
hasLoadedDateModule = false,

0 commit comments

Comments
 (0)