dqs-jquery-form-validator-rails 2.2.163 → 2.2.164
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +21 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/LICENSE +24 -0
- data/README.md +451 -0
- data/Rakefile +1 -0
- data/app/assets/javascripts/brazil.dev.js +115 -0
- data/app/assets/javascripts/brazil.js +9 -0
- data/app/assets/javascripts/date.dev.js +81 -0
- data/app/assets/javascripts/date.js +9 -0
- data/app/assets/javascripts/file.dev.js +413 -0
- data/app/assets/javascripts/file.js +9 -0
- data/app/assets/javascripts/html5.dev.js +168 -0
- data/app/assets/javascripts/html5.js +9 -0
- data/app/assets/javascripts/jquery.form-validator.min.js +9 -0
- data/app/assets/javascripts/jsconf.dev.js +55 -0
- data/app/assets/javascripts/jsconf.js +9 -0
- data/app/assets/javascripts/lang/cz.dev.js +65 -0
- data/app/assets/javascripts/lang/cz.js +9 -0
- data/app/assets/javascripts/lang/de.dev.js +65 -0
- data/app/assets/javascripts/lang/de.js +9 -0
- data/app/assets/javascripts/lang/es.dev.js +62 -0
- data/app/assets/javascripts/lang/es.js +9 -0
- data/app/assets/javascripts/lang/fr.dev.js +62 -0
- data/app/assets/javascripts/lang/fr.js +9 -0
- data/app/assets/javascripts/lang/it.dev.js +62 -0
- data/app/assets/javascripts/lang/it.js +9 -0
- data/app/assets/javascripts/lang/pl.dev.js +65 -0
- data/app/assets/javascripts/lang/pl.js +9 -0
- data/app/assets/javascripts/lang/pt.dev.js +65 -0
- data/app/assets/javascripts/lang/pt.js +9 -0
- data/app/assets/javascripts/lang/ro.dev.js +65 -0
- data/app/assets/javascripts/lang/ro.js +9 -0
- data/app/assets/javascripts/lang/ru.dev.js +66 -0
- data/app/assets/javascripts/lang/ru.js +9 -0
- data/app/assets/javascripts/lang/sv.dev.js +63 -0
- data/app/assets/javascripts/lang/sv.js +9 -0
- data/app/assets/javascripts/location.dev.js +78 -0
- data/app/assets/javascripts/location.js +9 -0
- data/app/assets/javascripts/sanitize.dev.js +154 -0
- data/app/assets/javascripts/sanitize.js +9 -0
- data/app/assets/javascripts/security.dev.js +523 -0
- data/app/assets/javascripts/security.js +9 -0
- data/app/assets/javascripts/src/core-validators.js +343 -0
- data/app/assets/javascripts/src/dialogs.js +123 -0
- data/app/assets/javascripts/src/jquery-plugins.js +452 -0
- data/app/assets/javascripts/src/module-loader.js +150 -0
- data/app/assets/javascripts/src/setup.js +168 -0
- data/app/assets/javascripts/src/utils.js +840 -0
- data/app/assets/javascripts/sweden.dev.js +213 -0
- data/app/assets/javascripts/sweden.js +9 -0
- data/app/assets/javascripts/theme-default.css +108 -0
- data/app/assets/javascripts/theme-default.min.css +1 -0
- data/app/assets/javascripts/toggleDisabled.dev.js +67 -0
- data/app/assets/javascripts/toggleDisabled.js +9 -0
- data/app/assets/javascripts/uk.dev.js +85 -0
- data/app/assets/javascripts/uk.js +9 -0
- data/dqs-jquery-form-validator.gemspec +27 -0
- data/lib/dqs-jquery-form-validator-rails.rb +1 -0
- data/lib/dqs/jquery/form/validator/rails.rb +13 -0
- data/lib/dqs/jquery/form/validator/rails/engine.rb +12 -0
- data/lib/dqs/jquery/form/validator/rails/version.rb +11 -0
- metadata +64 -2
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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 MIT
|
14
|
+
* @version 2.2.163
|
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 = (sum1 * 10) % 11;
|
41
|
+
if (remainder1 >= 10) {
|
42
|
+
remainder1 = 0;
|
43
|
+
}
|
44
|
+
if (remainder1 !== 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(cpf.substring(i - 1, i)) * (12 - i);
|
51
|
+
}
|
52
|
+
remainder2 = (sum2 * 10) % 11;
|
53
|
+
if (remainder2 >= 10) {
|
54
|
+
remainder2 = 0;
|
55
|
+
}
|
56
|
+
if (remainder2 !== parseInt(cpf.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
|
+
});
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* JQUERY-FORM-VALIDATOR
|
3
|
+
*
|
4
|
+
* @version 2.2.163
|
5
|
+
* @website http://formvalidator.net/
|
6
|
+
* @author Victor Jonsson, http://victorjonsson.se
|
7
|
+
* @license MIT
|
8
|
+
*/
|
9
|
+
$.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))?!1:!0},errorMessage:"",errorMessageKey:"badBrazilCPFAnswer"}),$.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)?!0:!1},errorMessage:"",errorMessageKey:"badBrazilTelephoneAnswer"}),$.formUtils.addValidator({name:"cep",validatorFunction:function(a){return a.match(/^(\d){5}([-. ]){0,1}(\d){3}$/g)?!0:!1},errorMessage:"",errorMessageKey:"badBrazilCEPAnswer"});
|
@@ -0,0 +1,81 @@
|
|
1
|
+
/**
|
2
|
+
* jQuery Form Validator Module: Date
|
3
|
+
* ------------------------------------------
|
4
|
+
* Created by Victor Jonsson <http://www.victorjonsson.se>
|
5
|
+
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
|
6
|
+
*
|
7
|
+
* The following validators will be added by this module:
|
8
|
+
* - Time (HH:mmm)
|
9
|
+
* - Birth date
|
10
|
+
*
|
11
|
+
* @website http://formvalidator.net/#location-validators
|
12
|
+
* @license MIT
|
13
|
+
* @version 2.2.163
|
14
|
+
*/
|
15
|
+
(function($) {
|
16
|
+
|
17
|
+
/*
|
18
|
+
* Validate time hh:mm
|
19
|
+
*/
|
20
|
+
$.formUtils.addValidator({
|
21
|
+
name : 'time',
|
22
|
+
validatorFunction : function(time) {
|
23
|
+
if (time.match(/^(\d{2}):(\d{2})$/) === null) {
|
24
|
+
return false;
|
25
|
+
} else {
|
26
|
+
var hours = parseInt(time.split(':')[0],10);
|
27
|
+
var minutes = parseInt(time.split(':')[1],10);
|
28
|
+
if( hours > 23 || minutes > 59 ) {
|
29
|
+
return false;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
return true;
|
33
|
+
},
|
34
|
+
errorMessage : '',
|
35
|
+
errorMessageKey: 'badTime'
|
36
|
+
});
|
37
|
+
|
38
|
+
/*
|
39
|
+
* Is this a valid birth date
|
40
|
+
*/
|
41
|
+
$.formUtils.addValidator({
|
42
|
+
name : 'birthdate',
|
43
|
+
validatorFunction : function(val, $el, conf) {
|
44
|
+
var dateFormat = 'yyyy-mm-dd';
|
45
|
+
if($el.valAttr('format')) {
|
46
|
+
dateFormat = $el.valAttr('format');
|
47
|
+
}
|
48
|
+
else if(typeof conf.dateFormat !== 'undefined') {
|
49
|
+
dateFormat = conf.dateFormat;
|
50
|
+
}
|
51
|
+
|
52
|
+
var inputDate = $.formUtils.parseDate(val, dateFormat);
|
53
|
+
if (!inputDate) {
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
|
57
|
+
var d = new Date();
|
58
|
+
var currentYear = d.getFullYear();
|
59
|
+
var year = inputDate[0];
|
60
|
+
var month = inputDate[1];
|
61
|
+
var day = inputDate[2];
|
62
|
+
|
63
|
+
if (year === currentYear) {
|
64
|
+
var currentMonth = d.getMonth() + 1;
|
65
|
+
if (month === currentMonth) {
|
66
|
+
var currentDay = d.getDate();
|
67
|
+
return day <= currentDay;
|
68
|
+
}
|
69
|
+
else {
|
70
|
+
return month < currentMonth;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
else {
|
74
|
+
return year < currentYear && year > (currentYear - 124); // we can not live for ever yet...
|
75
|
+
}
|
76
|
+
},
|
77
|
+
errorMessage : '',
|
78
|
+
errorMessageKey: 'badDate'
|
79
|
+
});
|
80
|
+
|
81
|
+
})(jQuery);
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* JQUERY-FORM-VALIDATOR
|
3
|
+
*
|
4
|
+
* @version 2.2.163
|
5
|
+
* @website http://formvalidator.net/
|
6
|
+
* @author Victor Jonsson, http://victorjonsson.se
|
7
|
+
* @license MIT
|
8
|
+
*/
|
9
|
+
!function(a){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?!1:!0},errorMessage:"",errorMessageKey:"badTime"}),a.formUtils.addValidator({name:"birthdate",validatorFunction:function(b,c,d){var e="yyyy-mm-dd";c.valAttr("format")?e=c.valAttr("format"):"undefined"!=typeof d.dateFormat&&(e=d.dateFormat);var f=a.formUtils.parseDate(b,e);if(!f)return!1;var g=new Date,h=g.getFullYear(),i=f[0],j=f[1],k=f[2];if(i===h){var l=g.getMonth()+1;if(j===l){var m=g.getDate();return m>=k}return l>j}return h>i&&i>h-124},errorMessage:"",errorMessageKey:"badDate"})}(jQuery);
|
@@ -0,0 +1,413 @@
|
|
1
|
+
/**
|
2
|
+
* jQuery Form Validator Module: File
|
3
|
+
* ------------------------------------------
|
4
|
+
* Created by Victor Jonsson <http://www.victorjonsson.se>
|
5
|
+
*
|
6
|
+
* The following validators will be added by this module:
|
7
|
+
* - mime type
|
8
|
+
* - file size
|
9
|
+
* - file extension
|
10
|
+
*
|
11
|
+
* @website http://formvalidator.net/#file-validators
|
12
|
+
* @license MIT
|
13
|
+
* @version 2.2.163
|
14
|
+
*/
|
15
|
+
(function($, window) {
|
16
|
+
|
17
|
+
'use strict';
|
18
|
+
|
19
|
+
var SUPPORTS_FILE_READER = typeof window.FileReader !== 'undefined',
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @return {Array}
|
23
|
+
*/
|
24
|
+
_getTypes = function($input) {
|
25
|
+
var allowedTypes = $.split( ($input.valAttr('allowing') || '').toLowerCase() );
|
26
|
+
if ($.inArray('jpg', allowedTypes) > -1 && $.inArray('jpeg', allowedTypes) === -1) {
|
27
|
+
allowedTypes.push('jpeg');
|
28
|
+
}
|
29
|
+
else if ($.inArray('jpeg', allowedTypes) > -1 && $.inArray('jpg', allowedTypes) === -1) {
|
30
|
+
allowedTypes.push('jpg');
|
31
|
+
}
|
32
|
+
return allowedTypes;
|
33
|
+
},
|
34
|
+
|
35
|
+
/**
|
36
|
+
* @param {Object} obj
|
37
|
+
* @param {String} key
|
38
|
+
* @param {String} insert
|
39
|
+
* @param {Object} lang
|
40
|
+
*/
|
41
|
+
_generateErrorMsg = function(obj, key, insert, lang) {
|
42
|
+
var msg = lang[key] || '';
|
43
|
+
obj.errorMessageKey = ''; // only use message attached to this object
|
44
|
+
obj.errorMessage = msg.replace('\%s', insert);
|
45
|
+
},
|
46
|
+
|
47
|
+
/**
|
48
|
+
* @param {String} msg
|
49
|
+
*/
|
50
|
+
_log = function(msg) {
|
51
|
+
if( window.console && window.console.log ) {
|
52
|
+
window.console.log(msg);
|
53
|
+
}
|
54
|
+
},
|
55
|
+
|
56
|
+
/**
|
57
|
+
* @param {String} imgPath
|
58
|
+
* @param {Function} successCallback
|
59
|
+
* @param {Function} errCallback
|
60
|
+
* @private
|
61
|
+
*/
|
62
|
+
_loadImage = function(imgPath, successCallback, errCallback) {
|
63
|
+
var reader = new FileReader(),
|
64
|
+
image = new Image();
|
65
|
+
|
66
|
+
reader.readAsDataURL(imgPath);
|
67
|
+
|
68
|
+
reader.onload = function(fileObj) {
|
69
|
+
|
70
|
+
image.onload = function() {
|
71
|
+
$(window).trigger('imageValidation', [this]);
|
72
|
+
successCallback(this);
|
73
|
+
};
|
74
|
+
|
75
|
+
image.onerror= function() {
|
76
|
+
errCallback();
|
77
|
+
};
|
78
|
+
|
79
|
+
image.src = fileObj.target.result;
|
80
|
+
|
81
|
+
};
|
82
|
+
};
|
83
|
+
|
84
|
+
/*
|
85
|
+
* Validate mime type (falls back on validate_extension in older browsers)
|
86
|
+
*/
|
87
|
+
$.formUtils.addValidator({
|
88
|
+
name : 'mime',
|
89
|
+
validatorFunction : function(str, $input, conf, language) {
|
90
|
+
|
91
|
+
if( SUPPORTS_FILE_READER ) {
|
92
|
+
var valid = true,
|
93
|
+
files = $input.get(0).files || [],
|
94
|
+
mime = '',
|
95
|
+
allowedTypes = _getTypes($input);
|
96
|
+
|
97
|
+
if( files.length ) {
|
98
|
+
$.each(files, function(i, file) {
|
99
|
+
valid = false;
|
100
|
+
mime = file.type || '';
|
101
|
+
$.each(allowedTypes, function(j, type) {
|
102
|
+
valid = mime.indexOf(type) > -1;
|
103
|
+
if( valid ) {
|
104
|
+
return false;
|
105
|
+
}
|
106
|
+
});
|
107
|
+
return valid;
|
108
|
+
});
|
109
|
+
|
110
|
+
if( !valid ) {
|
111
|
+
_log('Trying to upload a file with mime type '+mime+' which is not allowed');
|
112
|
+
_generateErrorMsg(this, 'wrongFileType', allowedTypes.join(', '), language);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
return valid;
|
117
|
+
|
118
|
+
} else {
|
119
|
+
_log('FileReader not supported by browser, will check file extension');
|
120
|
+
return $.formUtils.validators.validate_extension.validatorFunction(str, $input, conf, language);
|
121
|
+
}
|
122
|
+
},
|
123
|
+
errorMessage : '',
|
124
|
+
errorMessageKey: 'wrongFileType'
|
125
|
+
});
|
126
|
+
|
127
|
+
/**
|
128
|
+
* Validate file extension
|
129
|
+
*/
|
130
|
+
$.formUtils.addValidator({
|
131
|
+
name : 'extension',
|
132
|
+
validatorFunction : function(value, $input, conf, language) {
|
133
|
+
var valid = true,
|
134
|
+
_this = this,
|
135
|
+
allowedTypes = _getTypes($input);
|
136
|
+
|
137
|
+
$.each($input.get(0).files || [value], function(i, file) {
|
138
|
+
var val = typeof file === 'string' ? file : (file.value || file.fileName || file.name),
|
139
|
+
ext = val.substr( val.lastIndexOf('.')+1 );
|
140
|
+
|
141
|
+
if( $.inArray(ext.toLowerCase(), allowedTypes) === -1 ) {
|
142
|
+
valid = false;
|
143
|
+
_generateErrorMsg(_this, 'wrongFileType', allowedTypes.join(', '), language);
|
144
|
+
return false;
|
145
|
+
}
|
146
|
+
});
|
147
|
+
|
148
|
+
return valid;
|
149
|
+
},
|
150
|
+
errorMessage : '',
|
151
|
+
errorMessageKey: 'wrongFileType'
|
152
|
+
});
|
153
|
+
|
154
|
+
/**
|
155
|
+
* Validate file size
|
156
|
+
*/
|
157
|
+
$.formUtils.addValidator({
|
158
|
+
name : 'size',
|
159
|
+
validatorFunction : function(val, $input, conf, language) {
|
160
|
+
var maxSize = $input.valAttr('max-size');
|
161
|
+
if( !maxSize ) {
|
162
|
+
_log('Input "'+$input.attr('name')+'" is missing data-validation-max-size attribute');
|
163
|
+
return true;
|
164
|
+
} else if( !SUPPORTS_FILE_READER ) {
|
165
|
+
return true; // no fallback available
|
166
|
+
}
|
167
|
+
|
168
|
+
var maxBytes = $.formUtils.convertSizeNameToBytes(maxSize),
|
169
|
+
valid = true;
|
170
|
+
|
171
|
+
$.each($input.get(0).files || [], function(i, file) {
|
172
|
+
valid = file.size <= maxBytes;
|
173
|
+
return valid;
|
174
|
+
});
|
175
|
+
|
176
|
+
if( !valid ) {
|
177
|
+
_generateErrorMsg(this, 'wrongFileSize', maxSize, language);
|
178
|
+
}
|
179
|
+
return valid;
|
180
|
+
},
|
181
|
+
errorMessage : '',
|
182
|
+
errorMessageKey: 'wrongFileSize'
|
183
|
+
});
|
184
|
+
|
185
|
+
/**
|
186
|
+
* Make this function accessible via formUtils for unit tests
|
187
|
+
* @param {String} sizeName
|
188
|
+
* @return {Number}
|
189
|
+
*/
|
190
|
+
$.formUtils.convertSizeNameToBytes = function(sizeName) {
|
191
|
+
sizeName = sizeName.toUpperCase();
|
192
|
+
if( sizeName.substr(sizeName.length-1, 1) === 'M' ) {
|
193
|
+
return parseInt(sizeName.substr(0, sizeName.length-1), 10) * 1024 * 1024;
|
194
|
+
} else if( sizeName.substr(sizeName.length-2, 2) === 'MB' ) {
|
195
|
+
return parseInt(sizeName.substr(0, sizeName.length-2), 10) * 1024 * 1024;
|
196
|
+
} else if( sizeName.substr(sizeName.length-2, 2) === 'KB' ) {
|
197
|
+
return parseInt(sizeName.substr(0, sizeName.length-2), 10) * 1024;
|
198
|
+
} else if( sizeName.substr(sizeName.length-1, 1) === 'B' ) {
|
199
|
+
return parseInt(sizeName.substr(0, sizeName.length-1), 10);
|
200
|
+
} else {
|
201
|
+
return parseInt(sizeName, 10);
|
202
|
+
}
|
203
|
+
};
|
204
|
+
|
205
|
+
var disableFormSubmit = function() {
|
206
|
+
return false;
|
207
|
+
};
|
208
|
+
|
209
|
+
/**
|
210
|
+
* Attach dimension check onto formUtils only for unit testing purpose
|
211
|
+
* @param {HTMLImageElement} img
|
212
|
+
* @param {String} dimDeclaration
|
213
|
+
* @param {Boolean|String} Returns error message if image was invalid, false otherwise
|
214
|
+
*/
|
215
|
+
$.formUtils.checkImageDimension = function(img, dimDeclaration, language) {
|
216
|
+
var error = false,
|
217
|
+
restrictedDim = {width:0, height:0},
|
218
|
+
getDimRestriction = function(dimDeclaration) {
|
219
|
+
dimDeclaration = dimDeclaration.replace('min', '').replace('max', '');
|
220
|
+
var chunks = dimDeclaration.split('x');
|
221
|
+
restrictedDim.width = chunks[0];
|
222
|
+
restrictedDim.height = chunks[1] ? chunks[1] : chunks[0];
|
223
|
+
},
|
224
|
+
minDeclaration = false,
|
225
|
+
maxDeclaration = false,
|
226
|
+
declarationParts = dimDeclaration.split('-');
|
227
|
+
|
228
|
+
if( declarationParts.length === 1 ) {
|
229
|
+
if( declarationParts[0].indexOf('min') === 0 ) {
|
230
|
+
minDeclaration = declarationParts[0];
|
231
|
+
} else {
|
232
|
+
maxDeclaration = declarationParts[0];
|
233
|
+
}
|
234
|
+
} else {
|
235
|
+
minDeclaration = declarationParts[0];
|
236
|
+
maxDeclaration = declarationParts[1];
|
237
|
+
}
|
238
|
+
|
239
|
+
if( minDeclaration ) {
|
240
|
+
// check min
|
241
|
+
getDimRestriction(minDeclaration);
|
242
|
+
if( img.width < restrictedDim.width || img.height < restrictedDim.height ) {
|
243
|
+
error = language.imageTooSmall + ' ('+language.min+' '+restrictedDim.width+'x'+restrictedDim.height+'px)';
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
if( !error && maxDeclaration ) {
|
248
|
+
// Check max
|
249
|
+
getDimRestriction(maxDeclaration);
|
250
|
+
if( img.width > restrictedDim.width || img.height > restrictedDim.height ) {
|
251
|
+
if( img.width > restrictedDim.width ) {
|
252
|
+
error = language.imageTooWide +' '+restrictedDim.width+'px';
|
253
|
+
} else {
|
254
|
+
error = language.imageTooTall +' '+restrictedDim.height+'px';
|
255
|
+
}
|
256
|
+
error += ' ('+language.max+' '+restrictedDim.width+'x'+restrictedDim.height+'px)';
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
return error;
|
261
|
+
};
|
262
|
+
|
263
|
+
/**
|
264
|
+
* Attach ratio validation onto formUtils only for unit testing purpose
|
265
|
+
* @param {HTMLImageElement} img
|
266
|
+
* @param {String} dimDeclaration
|
267
|
+
* @param {Boolean|String} Returns error message if image was invalid, false otherwise
|
268
|
+
*/
|
269
|
+
$.formUtils.checkImageRatio = function(img, ratioDeclaration, language) {
|
270
|
+
var ratio = img.width / img.height,
|
271
|
+
calculateRatio = function(declaration) {
|
272
|
+
var dims = declaration.replace('max', '').replace('min', '').split(':');
|
273
|
+
return dims[0] / dims[1];
|
274
|
+
},
|
275
|
+
declarationParts = ratioDeclaration.split('-'),
|
276
|
+
isWithin = function(val, min, max) {
|
277
|
+
return val >= min && val <= max;
|
278
|
+
};
|
279
|
+
|
280
|
+
if ( declarationParts.length === 1 ) {
|
281
|
+
if ( ratio !== calculateRatio(declarationParts[0]) ) {
|
282
|
+
return language.imageRatioNotAccepted;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
else if( declarationParts.length === 2 && !isWithin(ratio, calculateRatio(declarationParts[0]), calculateRatio(declarationParts[1])) ) {
|
286
|
+
return language.imageRatioNotAccepted;
|
287
|
+
}
|
288
|
+
return false;
|
289
|
+
};
|
290
|
+
|
291
|
+
/**
|
292
|
+
* Validate image dimension
|
293
|
+
*/
|
294
|
+
$.formUtils.addValidator({
|
295
|
+
name : 'dimension',
|
296
|
+
validatorFunction : function(val, $input, conf, language, $form) {
|
297
|
+
var hasCorrectDim = false;
|
298
|
+
if( SUPPORTS_FILE_READER ) {
|
299
|
+
var file = $input.get(0).files || [];
|
300
|
+
hasCorrectDim = true;
|
301
|
+
|
302
|
+
if( $input.attr('data-validation').indexOf('mime') === -1) {
|
303
|
+
alert('You should validate file type being jpg, gif or png on input '+$input[0].name);
|
304
|
+
return false;
|
305
|
+
}
|
306
|
+
else if( file.length > 1 ) {
|
307
|
+
alert('Validating image dimensions does not support inputs allowing multiple files');
|
308
|
+
return false;
|
309
|
+
} else if( file.length === 0) {
|
310
|
+
return true;
|
311
|
+
}
|
312
|
+
|
313
|
+
if( $input.valAttr('has-valid-dim') ) {
|
314
|
+
return true;
|
315
|
+
}
|
316
|
+
else if( $input.valAttr('has-not-valid-dim') ) {
|
317
|
+
this.errorMessage = language.wrongFileDim + ' ' + $input.valAttr('has-not-valid-dim');
|
318
|
+
return false;
|
319
|
+
}
|
320
|
+
else if($.formUtils.eventType === 'keyup') {
|
321
|
+
return null;
|
322
|
+
}
|
323
|
+
|
324
|
+
var wasFormSubmit = false;
|
325
|
+
|
326
|
+
if( $.formUtils.isValidatingEntireForm ) {
|
327
|
+
wasFormSubmit = true;
|
328
|
+
$.formUtils.haltValidation = true;
|
329
|
+
$form
|
330
|
+
.bind('submit', disableFormSubmit)
|
331
|
+
.addClass('on-blur');
|
332
|
+
}
|
333
|
+
|
334
|
+
_loadImage(file[0], function(img) {
|
335
|
+
var error = false;
|
336
|
+
|
337
|
+
if ( $input.valAttr('dimension') ) {
|
338
|
+
error = $.formUtils.checkImageDimension(img, $input.valAttr('dimension'), language);
|
339
|
+
}
|
340
|
+
|
341
|
+
if ( !error && $input.valAttr('ratio') ) {
|
342
|
+
error = $.formUtils.checkImageRatio(img, $input.valAttr('ratio'), language);
|
343
|
+
}
|
344
|
+
|
345
|
+
// Set validation result flag on input
|
346
|
+
if( error ) {
|
347
|
+
$input.valAttr('has-not-valid-dim', error);
|
348
|
+
}
|
349
|
+
else {
|
350
|
+
$input.valAttr('has-valid-dim', 'true');
|
351
|
+
}
|
352
|
+
|
353
|
+
// Remove validation flag when input changed
|
354
|
+
if( !$input.valAttr('has-keyup-event') ) {
|
355
|
+
$input
|
356
|
+
.valAttr('has-keyup-event', '1')
|
357
|
+
.bind('keyup change', function(evt) {
|
358
|
+
if( evt.keyCode !== 9 && evt.keyCode !== 16 ) {
|
359
|
+
$(this)
|
360
|
+
.valAttr('has-not-valid-dim', false)
|
361
|
+
.valAttr('has-valid-dim', false);
|
362
|
+
}
|
363
|
+
});
|
364
|
+
}
|
365
|
+
|
366
|
+
if( wasFormSubmit ) {
|
367
|
+
$.formUtils.haltValidation = false;
|
368
|
+
$form
|
369
|
+
.removeClass('on-blur')
|
370
|
+
.get(0).onsubmit = function() {};
|
371
|
+
|
372
|
+
$form.unbind('submit', disableFormSubmit);
|
373
|
+
$form.trigger('submit'); // fire submit once more
|
374
|
+
|
375
|
+
} else {
|
376
|
+
$input.trigger('blur'); // triggers the validation once more
|
377
|
+
}
|
378
|
+
|
379
|
+
}, function(err) {
|
380
|
+
throw err;
|
381
|
+
});
|
382
|
+
|
383
|
+
return true;
|
384
|
+
}
|
385
|
+
|
386
|
+
return hasCorrectDim;
|
387
|
+
},
|
388
|
+
errorMessage : '',
|
389
|
+
errorMessageKey: '' // error message created dynamically
|
390
|
+
// errorMessageKey: 'wrongFileDim'
|
391
|
+
});
|
392
|
+
|
393
|
+
/*
|
394
|
+
* This event listener will remove error messages for file
|
395
|
+
* inputs when file changes
|
396
|
+
*/
|
397
|
+
$(window).one('validatorsLoaded formValidationSetup', function(evt, $form) {
|
398
|
+
var $inputs;
|
399
|
+
if( $form ) {
|
400
|
+
$inputs = $form.find('input[type="file"]');
|
401
|
+
} else {
|
402
|
+
$inputs = $('input[type="file"]');
|
403
|
+
}
|
404
|
+
|
405
|
+
$inputs.filter('*[data-validation]').bind('change', function() {
|
406
|
+
$(this)
|
407
|
+
.removeClass('error')
|
408
|
+
.parent()
|
409
|
+
.find('.form-error').remove();
|
410
|
+
});
|
411
|
+
});
|
412
|
+
|
413
|
+
})(jQuery, window);
|