Skip to content

Commit 67c42a9

Browse files
committed
Now possible to validate file size, extension and type using the file module
1 parent b41dad6 commit 67c42a9

16 files changed

+244
-56
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Read the documentation for the default features at [http://formvalidator.net/#de
7474
* **spamcheck**
7575
* **confirmation**
7676
* **strength***Validate the strength of a password (strength strength3)*
77-
* **backend***Validate value of input on backend*
77+
* **backend***Validate value of input on server side*
7878

7979
Read the documentation for the security module at [http://formvalidator.net/#security-validators](http://formvalidator.net/#security-validators)
8080

@@ -93,6 +93,13 @@ Read the documentation for the date module at [http://formvalidator.net/#date-va
9393

9494
Read the documentation for the location module at [http://formvalidator.net/#location-validators](http://formvalidator.net/#location-validators)
9595

96+
### Module: file
97+
* **mime**
98+
* **extension**
99+
* **size**
100+
101+
Read the documentation for the file module at [http://formvalidator.net/#file-validators](http://formvalidator.net/#file-validators)
102+
96103
### Module: sweden
97104
* **swemob***validate that the value is a swedish mobile telephone number*
98105
* **swesec***validate swedish social security number*
@@ -227,14 +234,18 @@ var enErrorDialogs = {
227234
badLength : 'You have to give an answer between ',
228235
notConfirmed : 'Values could not be confirmed',
229236
badDomain : 'Incorrect domain value',
230-
badUrl : 'Incorrect url value',
231-
badFloat : 'Incorrect float value',
237+
badUrl : 'The answer you gave was not a correct URL',
232238
badCustomVal : 'You gave an incorrect answer',
233-
badInt : 'Incorrect integer value',
239+
badInt : 'The answer you gave was not a correct number',
234240
badSecurityNumber : 'Your social security number was incorrect',
235241
badUKVatAnswer : 'Incorrect UK VAT Number',
242+
badStrength : 'The password isn\'t strong enough',
236243
badNumberOfSelectedOptionsStart : 'You have to choose at least ',
237-
badNumberOfSelectedOptionsEnd : ' answers'
244+
badNumberOfSelectedOptionsEnd : ' answers',
245+
badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ',
246+
badAlphaNumericExtra: ' and ',
247+
wrongFileSize : 'The file you are trying to upload is too large',
248+
wrongFileType : 'The file you are trying to upload is of wrong type'
238249
};
239250
```
240251

@@ -269,6 +280,9 @@ that attribute will be displayed instead of the error dialog that the validation
269280

270281
## Changelog
271282

283+
#### 2.0.7
284+
* Now possible to validate file size, extension and mime type (using the file module)
285+
272286
#### 2.0
273287
* [min|max]_length is removed (now merged with length validation).
274288
* The number, int and float validation is merged together, all three variants is now validated by the number validation.

form-validator/date.dev.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
* Created by Victor Jonsson <http://www.victorjonsson.se>
55
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
66
*
7-
* This form validation module adds validators used to validate date
8-
* and time values. The following validators will be added by
9-
* this module:
10-
* - validate_time
11-
* - validate_birthdate
12-
*
7+
* The following validators will be added by this module:
8+
* - Time (HH:mmm)
9+
* - Birth date
1310
*
11+
* @website http://formvalidator.net/#location-validators
1412
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 2.0.3
13+
* @version 2.0.7
1614
*/
1715
(function($) {
1816

1917
/*
2018
* Validate time hh:mm
2119
*/
2220
$.formUtils.addValidator({
23-
name : 'validate_time',
21+
name : 'time',
2422
validate : function(time) {
2523
if (time.match(/^(\d{2}):(\d{2})$/) === null) {
2624
return false;
@@ -41,7 +39,7 @@
4139
* Is this a valid birth date
4240
*/
4341
$.formUtils.addValidator({
44-
name : 'validate_birthdate',
42+
name : 'birthdate',
4543
validate : function(val, $el, conf) {
4644
var dateFormat = 'yyyy-mm-dd';
4745
if($el.valAttr('format')) {

form-validator/date.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/file.dev.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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/
12+
* @license Dual licensed under the MIT or GPL Version 2 licenses
13+
* @version 2.0.7
14+
*/
15+
(function($, window) {
16+
17+
var SUPPORTS_FILE_READER = typeof window.FileReader != 'undefined',
18+
19+
/**
20+
* @return {Array}
21+
*/
22+
_getTypes = function($input) {
23+
var allowedTypes = $.split( ($input.valAttr('allowing') || '').toLowerCase() );
24+
25+
if( $.inArray('jpg', allowedTypes) > -1 && $.inArray('jpeg', allowedTypes) == -1)
26+
allowedTypes.push('jpeg');
27+
else if( $.inArray('jpeg', allowedTypes) > -1 && $.inArray('jpg', allowedTypes) == -1)
28+
allowedTypes.push('jpg');
29+
return allowedTypes;
30+
};
31+
32+
/*
33+
* Validate mime type (falls back on validate_extension in older browsers)
34+
*/
35+
$.formUtils.addValidator({
36+
name : 'mime',
37+
validate : function(str, $input) {
38+
var files = $input.get(0).files || [];
39+
40+
if( SUPPORTS_FILE_READER ) {
41+
var valid = true,
42+
mime = '',
43+
allowedTypes = _getTypes($input);
44+
45+
$.each(files, function(i, file) {
46+
valid = false;
47+
mime = file.type || '';
48+
$.each(allowedTypes, function(j, type) {
49+
valid = mime.indexOf(type) > -1;
50+
if( valid ) {
51+
return false;
52+
}
53+
});
54+
return valid;
55+
});
56+
return valid;
57+
58+
} else {
59+
return $.formUtils.validators.extension.validate(str, $input);
60+
}
61+
},
62+
errorMessage : 'The file you are trying to upload is of wrong type',
63+
errorMessageKey: 'wrongFileType'
64+
});
65+
66+
/**
67+
* Validate file extension
68+
*/
69+
$.formUtils.addValidator({
70+
name : 'extension',
71+
validate : function(val, $input) {
72+
var ext = val.substr( val.lastIndexOf('.')+1 );
73+
return $.inArray(ext.toLowerCase(), _getTypes($input)) > -1;
74+
},
75+
errorMessage : 'The file you are trying to upload is of wrong type',
76+
errorMessageKey: 'wrongFileType'
77+
});
78+
79+
/**
80+
* Validate file size
81+
*/
82+
$.formUtils.addValidator({
83+
name : 'size',
84+
validate : function(val, $input) {
85+
var maxSize = $input.valAttr('max-size');
86+
if( !maxSize ) {
87+
console.log('Input "'+$input.attr('name')+'" is missing data-validation-max-size attribute');
88+
return true;
89+
} else if( !SUPPORTS_FILE_READER ) {
90+
return true; // no fallback available
91+
}
92+
93+
var maxBytes = $.formUtils.convertSizeNameToBytes(maxSize),
94+
valid = true;
95+
$.each($input.get(0).files || [], function(i, file) {
96+
valid = file.size <= maxBytes;
97+
return valid;
98+
});
99+
return valid;
100+
},
101+
errorMessage : 'The file you are trying to upload is too large',
102+
errorMessageKey: 'wrongFileSize'
103+
});
104+
105+
/**
106+
* Make this function accessible via formUtils for unit tests
107+
* @param {String} sizeName
108+
* @return {Number}
109+
*/
110+
$.formUtils.convertSizeNameToBytes = function(sizeName) {
111+
sizeName = sizeName.toUpperCase();
112+
if( sizeName.substr(sizeName.length-1, 1) == 'M' ) {
113+
return parseInt(sizeName.substr(0, sizeName.length-1), 10) * 1024 * 1024;
114+
} else if( sizeName.substr(sizeName.length-2, 2) == 'MB' ) {
115+
return parseInt(sizeName.substr(0, sizeName.length-2), 10) * 1024 * 1024;
116+
} else if( sizeName.substr(sizeName.length-2, 2) == 'KB' ) {
117+
return parseInt(sizeName.substr(0, sizeName.length-2), 10) * 1024;
118+
} else if( sizeName.substr(sizeName.length-1, 1) == 'B' ) {
119+
return parseInt(sizeName.substr(0, sizeName.length-1), 10);
120+
} else {
121+
return parseInt(sizeName, 10);
122+
}
123+
};
124+
125+
/*
126+
* This event listener will remove error messages for file
127+
* inputs when file changes
128+
*/
129+
$.formUtils.on('load', function() {
130+
$('input[type="file"]').filter('*[data-validation]').bind('change', function() {
131+
$(this).parent().find('.form-error').remove();
132+
});
133+
});
134+
135+
})(jQuery, window);

form-validator/file.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/jquery.form-validator.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* jQuery Form Validator
33
* ------------------------------------------
44
* Created by Victor Jonsson <http://www.victorjonsson.se>
5-
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
65
*
6+
* @website http://formvalidator.net/
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 2.0.3
8+
* @version 2.0.7
99
*/
1010
(function($) {
1111

@@ -512,7 +512,7 @@
512512
* @param {Object} validator
513513
*/
514514
addValidator : function(validator) {
515-
var name = validator.name.indexOf('validate_') === 0 ? validator.name : 'validate_'+validator.name;
515+
var name = validator.name.indexOf('validate_') === 0 ? validator.name : 'validate_'+validator.name; // legacy...
516516
this.validators[name] = validator;
517517
},
518518

@@ -522,6 +522,8 @@
522522
*/
523523
on : function(evt, callback) {
524524
// Why not use $(document).bind('validators.loaded', func);
525+
if( this._events[evt] === undefined )
526+
this._events[evt] = [];
525527
this._events[evt].push(callback);
526528
},
527529

@@ -531,7 +533,7 @@
531533
* @param [argB]
532534
*/
533535
trigger : function(evt, argA, argB) {
534-
$.each(this._events[evt], function(i, func) {
536+
$.each(this._events[evt] || [], function(i, func) {
535537
func(argA, argB);
536538
});
537539
},
@@ -1070,7 +1072,9 @@
10701072
badNumberOfSelectedOptionsStart : 'You have to choose at least ',
10711073
badNumberOfSelectedOptionsEnd : ' answers',
10721074
badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ',
1073-
badAlphaNumericExtra: ' and '
1075+
badAlphaNumericExtra: ' and ',
1076+
wrongFileSize : 'The file you are trying to upload is too large',
1077+
wrongFileType : 'The file you are trying to upload is of wrong type'
10741078
}
10751079
};
10761080

form-validator/jquery.form-validator.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/location.dev.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
* jQuery Form Validator Module: Date
33
* ------------------------------------------
44
* Created by Victor Jonsson <http://www.victorjonsson.se>
5-
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
65
*
76
* The following validators will be added by this module:
8-
* - validate_country
9-
* - validate_state
10-
* - validate_longlat
11-
*
7+
* - Country
8+
* - US state
9+
* - longitude and latitude
1210
*
11+
* @website http://formvalidator.net/#location-validators
1312
* @license Dual licensed under the MIT or GPL Version 2 licenses
14-
* @version 2.0.3
13+
* @version 2.0.7
1514
*/
1615
(function($) {
1716

1817
/*
1918
* Validate that country exists
2019
*/
2120
$.formUtils.addValidator({
22-
name : 'validate_country',
21+
name : 'country',
2322
validate : function(str) {
2423
return $.inArray(str.toLowerCase(), this.countries) > -1;
2524
},
@@ -32,7 +31,7 @@
3231
* Is this a valid federate state in the US
3332
*/
3433
$.formUtils.addValidator({
35-
name : 'validate_federatestate',
34+
name : 'federatestate',
3635
validate : function(str) {
3736
return $.inArray(str.toLowerCase(), this.states) > -1;
3837
},
@@ -43,7 +42,7 @@
4342

4443

4544
$.formUtils.addValidator({
46-
name : 'validate_longlat',
45+
name : 'longlat',
4746
validate : function(str) {
4847
var regexp = /^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/;
4948
return regexp.test(str);

0 commit comments

Comments
 (0)