Skip to content

Commit 135b65a

Browse files
committed
Better error messages for file validations
1 parent 24075ee commit 135b65a

12 files changed

+58
-37
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,28 +234,30 @@ var enErrorDialogs = {
234234
badTelephone : 'You have not given a correct phone number',
235235
badSecurityAnswer : 'You have not given a correct answer to the security question',
236236
badDate : 'You have not given a correct date',
237-
lengthBadStart : 'You must give an answer between ',
238-
lengthBadEnd : 'characters',
239-
lengthTooLongStart : 'You have given an answer longer than ',
240-
lengthTooShortStart : 'You have given an answer shorter than ',
241-
notConfirmed : 'Values could not be confirmed',
237+
lengthBadStart : 'The input value must be between ',
238+
lengthBadEnd : ' characters',
239+
lengthTooLongStart : 'The input value is longer than ',
240+
lengthTooShortStart : 'The input value is shorter than ',
241+
notConfirmed : 'Input values could not be confirmed',
242242
badDomain : 'Incorrect domain value',
243-
badUrl : 'The answer you gave was not a correct URL',
244-
badCustomVal : 'You gave an incorrect answer',
245-
badInt : 'The answer you gave was not a correct number',
243+
badUrl : 'The input value is not a correct URL',
244+
badCustomVal : 'The input value is incorrect',
245+
badInt : 'The input value was not a correct number',
246246
badSecurityNumber : 'Your social security number was incorrect',
247247
badUKVatAnswer : 'Incorrect UK VAT Number',
248248
badStrength : 'The password isn\'t strong enough',
249249
badNumberOfSelectedOptionsStart : 'You have to choose at least ',
250250
badNumberOfSelectedOptionsEnd : ' answers',
251-
badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ',
251+
badAlphaNumeric : 'The input value can only contain alphanumeric characters ',
252252
badAlphaNumericExtra: ' and ',
253-
wrongFileSize : 'The file you are trying to upload is too large',
254-
wrongFileType : 'The file you are trying to upload is of wrong type',
255-
groupCheckedTooFewStart : 'Please choose at least ',
256-
groupCheckedTooManyStart : 'Please choose a maximum of ',
253+
wrongFileSize : 'The file you are trying to upload is too large (max %s)',
254+
wrongFileType : 'Only files of type %s is allowed',
257255
groupCheckedRangeStart : 'Please choose between ',
258-
groupCheckedEnd : ' item(s)'
256+
groupCheckedTooFewStart : 'Please choose at least ',
257+
groupCheckedTooManyStart : 'Please choose a maximum of ',
258+
groupCheckedEnd : ' item(s)',
259+
badCreditCard : 'The credit card number is not correct',
260+
badCVV : 'The CVV number was not correct'
259261
};
260262
```
261263

form-validator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.2.beta.30
13+
* @version 2.2.beta.31
1414
*/
1515
(function($) {
1616

form-validator/file.dev.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.2.beta.30
13+
* @version 2.2.beta.31
1414
*/
1515
(function($, window) {
1616

@@ -19,7 +19,7 @@
1919
/**
2020
* @return {Array}
2121
*/
22-
_getTypes = function($input) {
22+
_getTypes = function($input) {
2323
var allowedTypes = $.split( ($input.valAttr('allowing') || '').toLowerCase() );
2424

2525
if( $.inArray('jpg', allowedTypes) > -1 && $.inArray('jpeg', allowedTypes) == -1)
@@ -29,10 +29,22 @@
2929
return allowedTypes;
3030
},
3131

32+
/**
33+
* @param {Object} obj
34+
* @param {String} key
35+
* @param {String} insert
36+
* @param {Object} lang
37+
*/
38+
_generateErrorMsg = function(obj, key, insert, lang) {
39+
var msg = lang[key];
40+
obj.errorMessageKey = ''; // only use message attached to this object
41+
obj.errorMessage = msg.replace('\%s', insert);
42+
},
43+
3244
/**
3345
* @param {String} msg
3446
*/
35-
_log = function(msg) {
47+
_log = function(msg) {
3648
if( window.console && window.console.log ) {
3749
window.console.log(msg);
3850
}
@@ -43,7 +55,7 @@
4355
*/
4456
$.formUtils.addValidator({
4557
name : 'mime',
46-
validatorFunction : function(str, $input) {
58+
validatorFunction : function(str, $input, conf, language) {
4759

4860
if( SUPPORTS_FILE_READER ) {
4961
var valid = true,
@@ -66,17 +78,18 @@
6678

6779
if( !valid ) {
6880
_log('Trying to upload a file with mime type '+mime+' which is not allowed');
81+
_generateErrorMsg(this, 'wrongFileType', allowedTypes.join(', '), language);
6982
}
7083
}
7184

7285
return valid;
73-
86+
7487
} else {
7588
_log('FileReader not supported by browser, will check file extension');
7689
return $.formUtils.validators.validate_extension.validatorFunction(str, $input);
7790
}
7891
},
79-
errorMessage : 'The file you are trying to upload is of wrong type',
92+
errorMessage : '',
8093
errorMessageKey: 'wrongFileType'
8194
});
8295

@@ -95,12 +108,13 @@
95108

96109
if( $.inArray(ext.toLowerCase(), types) == -1 ) {
97110
valid = false;
111+
_generateErrorMsg(this, 'wrongFileType', allowedTypes.join(', '), language);
98112
return false;
99113
}
100114
});
101115
return valid;
102116
},
103-
errorMessage : 'The file you are trying to upload is of wrong type',
117+
errorMessage : '',
104118
errorMessageKey: 'wrongFileType'
105119
});
106120

@@ -109,7 +123,7 @@
109123
*/
110124
$.formUtils.addValidator({
111125
name : 'size',
112-
validatorFunction : function(val, $input) {
126+
validatorFunction : function(val, $input, conf, language) {
113127
var maxSize = $input.valAttr('max-size');
114128
if( !maxSize ) {
115129
_log('Input "'+$input.attr('name')+'" is missing data-validation-max-size attribute');
@@ -120,13 +134,18 @@
120134

121135
var maxBytes = $.formUtils.convertSizeNameToBytes(maxSize),
122136
valid = true;
137+
123138
$.each($input.get(0).files || [], function(i, file) {
124139
valid = file.size <= maxBytes;
125140
return valid;
126141
});
142+
143+
if( !valid ) {
144+
_generateErrorMsg(this, 'wrongFileSize', maxSize, language);
145+
}
127146
return valid;
128147
},
129-
errorMessage : 'The file you are trying to upload is too large',
148+
errorMessage : '',
130149
errorMessageKey: 'wrongFileSize'
131150
});
132151

@@ -166,7 +185,7 @@
166185
$(this)
167186
.removeClass('error')
168187
.parent()
169-
.find('.form-error').remove();
188+
.find('.form-error').remove();
170189
});
171190
});
172191

form-validator/file.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/form-test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<label class="control-label">File validation</label>
173173
<input type="file" name="some-file1" class="form-control"
174174
data-validation="size mime required"
175-
data-validation-size-error-msg="The file cant be larger than 400kb"
175+
data-validation-error-msg-size="The file cant be larger than 400kb"
176176
data-validation-error-msg="You must upload an image file (max 400 kb)"
177177
data-validation-allowing="jpg, png, ico"
178178
data-validation-max-size="400kb" />

form-validator/html5.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @website http://formvalidator.net/
1919
* @license Dual licensed under the MIT or GPL Version 2 licenses
20-
* @version 2.2.beta.30
20+
* @version 2.2.beta.31
2121
*/
2222
(function($, window) {
2323

form-validator/jquery.form-validator.js

Lines changed: 3 additions & 3 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.beta.30
8+
* @version 2.2.beta.31
99
*/
1010
(function($) {
1111

@@ -1338,8 +1338,8 @@
13381338
badNumberOfSelectedOptionsEnd : ' answers',
13391339
badAlphaNumeric : 'The input value can only contain alphanumeric characters ',
13401340
badAlphaNumericExtra: ' and ',
1341-
wrongFileSize : 'The file you are trying to upload is too large',
1342-
wrongFileType : 'The file you are trying to upload is of wrong type',
1341+
wrongFileSize : 'The file you are trying to upload is too large (max %s)',
1342+
wrongFileType : 'Only files of type %s is allowed',
13431343
groupCheckedRangeStart : 'Please choose between ',
13441344
groupCheckedTooFewStart : 'Please choose at least ',
13451345
groupCheckedTooManyStart : 'Please choose a maximum of ',

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

Lines changed: 2 additions & 2 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.2.beta.30
13+
* @version 2.2.beta.31
1414
*/
1515
(function($) {
1616

form-validator/security.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* - cvv
1414
*
1515
* @website http://formvalidator.net/#security-validators
16-
* @version 2.2.beta.30
16+
* @version 2.2.beta.31
1717
*/
1818
(function($, window) {
1919

form-validator/sweden.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @website http://formvalidator.net/#swedish-validators
1515
* @license Dual licensed under the MIT or GPL Version 2 licenses
16-
* @version 2.2.beta.30
16+
* @version 2.2.beta.31
1717
*/
1818
(function($, window) {
1919

form-validator/uk.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @website http://formvalidator.net/#uk-validators
1111
* @license Dual licensed under the MIT or GPL Version 2 licenses
12-
* @version 2.2.beta.30
12+
* @version 2.2.beta.31
1313
*/
1414
$.formUtils.addValidator({
1515
name : 'ukvatnumber',

0 commit comments

Comments
 (0)