Skip to content

Commit 0c25635

Browse files
committed
Added sanitation module victorjonsson#284 and minified portuguese lang victorjonsson#288
1 parent d0cae7c commit 0c25635

22 files changed

+231
-19
lines changed

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.43
13+
* @version 2.2.48
1414
*/
1515
(function($) {
1616

form-validator/file.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/#file-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.2.43
13+
* @version 2.2.48
1414
*/
1515
(function($, window) {
1616

form-validator/form-test.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ <h2>HTML5 attributes</h2>
359359
errorMessagePosition : messagePosition,
360360
scrollToTopOnError : true,
361361
lang : 'sv',
362+
sanitizeAll : 'trim', // only used on form C
362363
// borderColorOnError : 'purple',
363364
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', uk'+dev +( xtraModule ? ','+xtraModule:''),
364365
onModulesLoaded: function() {
@@ -392,7 +393,7 @@ <h2>HTML5 attributes</h2>
392393

393394
window.applyValidation(true, '#form-a', 'top');
394395
window.applyValidation(false, '#form-b', 'element');
395-
window.applyValidation(true, '#form-c', $('#error-container'));
396+
window.applyValidation(true, '#form-c', $('#error-container'), 'sanitize'+dev);
396397
window.applyValidation(true, '#form-d', 'element', 'html5'+dev);
397398

398399
// Load one module outside $.validate() even though you do not have to

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.43
20+
* @version 2.2.48
2121
*/
2222
(function($, window) {
2323

form-validator/jquery.form-validator.js

Lines changed: 1 addition & 1 deletion
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.43
8+
* @version 2.2.48
99
*/
1010
(function ($) {
1111

form-validator/jquery.form-validator.min.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/jsconf.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @website http://formvalidator.net/#location-validators
99
* @license Dual licensed under the MIT or GPL Version 2 licenses
10-
* @version 2.2.43
10+
* @version 2.2.48
1111
*/
1212
(function($) {
1313

form-validator/lang/de.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.43
9+
* @version 2.2.48
1010
*/
1111
(function($, window) {
1212

form-validator/lang/es.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.43
9+
* @version 2.2.48
1010
*/
1111
(function($, window) {
1212

form-validator/lang/fr.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.43
9+
* @version 2.2.48
1010
*/
1111
(function($, window) {
1212

form-validator/lang/pt.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.43
9+
* @version 2.2.48
1010
*/
1111
(function($, window) {
1212

form-validator/lang/pt.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/lang/sv.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @website http://formvalidator.net/
88
* @license Dual licensed under the MIT or GPL Version 2 licenses
9-
* @version 2.2.43
9+
* @version 2.2.48
1010
*/
1111
(function($, window) {
1212

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.43
13+
* @version 2.2.48
1414
*/
1515
(function($) {
1616

form-validator/qunit.html

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,84 @@
679679
*/
680680
});
681681

682+
test('Sanitation', function() {
683+
clearForm();
684+
685+
var data = {
686+
trim : {
687+
val : ' apa ',
688+
expected : 'apa'
689+
},
690+
trimLeft : {
691+
val : ' apa ',
692+
expected: 'apa '
693+
},
694+
trimRight : {
695+
val : ' apa ',
696+
expected: ' apa'
697+
},
698+
capitalize : {
699+
val : 'En groda som Önskar sig öl',
700+
expected: 'En Groda Som Önskar Sig Öl'
701+
},
702+
'capitalize trim' : {
703+
val : ' En groda som Önskar sig öl ',
704+
expected: 'En Groda Som Önskar Sig Öl'
705+
},
706+
upper : {
707+
val : ' apa ',
708+
expected: ' APA '
709+
},
710+
lower : {
711+
val : ' aPA ',
712+
expected: ' apa '
713+
},
714+
insertLeft : {
715+
val : ' apa ',
716+
expected: 'left... apa ',
717+
attr : {
718+
'data-sanitize-insert-right' : 'right...',
719+
'data-sanitize-insert-left' : 'left...'
720+
}
721+
},
722+
insertRight : {
723+
val : ' apa ',
724+
expected: ' apa right...',
725+
attr : {
726+
'data-sanitize-insert-right' : 'right...',
727+
'data-sanitize-insert-left' : 'left...'
728+
}
729+
},
730+
'trim insertRight' : {
731+
val : ' apa ',
732+
expected: 'aparight...',
733+
attr : {
734+
'data-sanitize-insert-right' : 'right...'
735+
}
736+
},
737+
escape : {
738+
val : '<p style="color: pink">hejsan & så\' vidare</p>',
739+
expected: '&lt;p style=&quot;color: pink&quot;&gt;hejsan &amp; så&#8217; vidare&lt;/p&gt;' }
740+
};
741+
742+
$.each(data, function(sanitation, data) {
743+
var $input = input('hejsan', {'':''}, {name:'test'});
744+
$input.attr('data-sanitize', sanitation);
745+
$.each(data.attr || {}, function(attrName, attrVal) {
746+
$input.attr(attrName, attrVal);
747+
});
748+
$.formUtils.setupSanitation($form, $.formUtils.defaultConfig());
749+
$input.val(data.val);
750+
$input.trigger('blur');
751+
equal(
752+
$input.val(),
753+
data.expected,
754+
'Sanitize with "'+sanitation+'"'
755+
);
756+
});
757+
758+
});
759+
682760
test('Confirmation', function() {
683761

684762
clearForm();
@@ -730,7 +808,7 @@
730808
var dev = window.location.hash.indexOf('dev') > -1 ? '.dev' : '';
731809

732810
$.validate({
733-
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', date'+dev+', uk'+dev,
811+
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', date'+dev+', sanitize'+dev+', uk'+dev,
734812
onModulesLoaded: function( $form ) {
735813
if( window.console && window.console.log )
736814
console.log('About to run all tests');

form-validator/sanitize.dev.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* jQuery Form Validator Module: sanitize
3+
* ------------------------------------------
4+
* Created by Victor Jonsson <http://www.victorjonsson.se>
5+
*
6+
* This module makes it possible to add sanitation functions to
7+
* inputs. The functions is triggered on blur. Example:
8+
*
9+
* <input data-sanitize="uppercase trim" />
10+
*
11+
* Available functions are:
12+
* - uppercase
13+
* - lowercase
14+
* - capitalize
15+
* - trim
16+
* - trimLeft
17+
* - trimRight
18+
* - numberFormat
19+
* - insertLeft
20+
* - insertRight
21+
* - escape (replace <, >, &, ' and " with HTML entities)
22+
*
23+
* @website http://formvalidator.net/
24+
* @license Dual licensed under the MIT or GPL Version 2 licenses
25+
* @version 2.2.48
26+
*/
27+
(function($, window) {
28+
29+
"use strict";
30+
31+
var inputsThatCantBeSanitized = '[type="button"], [type="submit"], [type="radio"], [type="checkbox"], [type="reset"], [type="search"]',
32+
splitStringInHalf = function(str) {
33+
var firstHalf = Math.floor(str.length / 2);
34+
return [str.substr(0, firstHalf), str.substr(firstHalf, str.length - firstHalf)];
35+
},
36+
sanitizeCommands = {
37+
upper : function(val) {
38+
return val.toLocaleUpperCase();
39+
},
40+
lower : function(val) {
41+
return val.toLocaleLowerCase();
42+
},
43+
trim : function(val) {
44+
return $.trim(val);
45+
},
46+
trimLeft : function(val) {
47+
if( val.length > 1 ) {
48+
var parts = splitStringInHalf(val);
49+
val = $.trim(parts[0]) + parts[1];
50+
}
51+
return val;
52+
},
53+
trimRight : function(val) {
54+
if( val.length > 1 ) {
55+
var parts = splitStringInHalf(val);
56+
val = parts[0] + $.trim(parts[1]);
57+
}
58+
return val;
59+
},
60+
capitalize : function(val) {
61+
var words = val.split(' ');
62+
$.each(words, function(i, word) {
63+
words[i] = word.substr(0,1).toUpperCase() + word.substr(1, word.length);
64+
});
65+
return words.join(' ');
66+
},
67+
insertRight : function(val, $input) {
68+
return val + ($input.attr('data-sanitize-insert-right') || '').replace(/\[SPACE\]/g, ' ');
69+
},
70+
insertLeft : function(val, $input) {
71+
return ($input.attr('data-sanitize-insert-left') || '').replace(/\[SPACE\]/g, ' ') + val;
72+
},
73+
numberFormat : function(val, $input, config) {
74+
if( 'numeral' in window ) {
75+
val = numeral(val).format( $input.attr('data-sanitation-format') );
76+
} else {
77+
throw new Error('Using sanitation function "numberFormat" requires that you include numeraljs (http://http://numeraljs.com/)');
78+
}
79+
return val;
80+
},
81+
escape : function(val) {
82+
var symbols = {
83+
'<' : '__%AMP%__lt;',
84+
'>' : '__%AMP%__gt;',
85+
'&' : '__%AMP%__amp;',
86+
'\'': '__%AMP%__#8217;',
87+
'"' : '__%AMP%__quot;'
88+
};
89+
$.each(symbols, function(symbol, replacement) {
90+
val = val.replace(new RegExp(symbol, 'g'), replacement);
91+
});
92+
return val.replace(new RegExp('__\%AMP\%__', 'g'), '&');
93+
}
94+
},
95+
setupSanitation = function(evt, $forms, config) {
96+
97+
if( !$forms ) {
98+
$forms = $('form');
99+
}
100+
101+
var execSanitationCommands = function() {
102+
var $input = $(this),
103+
value = $input.val();
104+
$.split($input.attr('data-sanitize'), function(command) {
105+
if( command in sanitizeCommands )
106+
value = sanitizeCommands[command](value, $input, config);
107+
else
108+
throw new Error('Use of unknown sanitize command "'+command+'"');
109+
});
110+
$input.val(value);
111+
};
112+
113+
$forms.each(function() {
114+
var $form = $(this);
115+
if( config.sanitizeAll ) {
116+
$form.find('input,textarea').not(inputsThatCantBeSanitized).attr('data-sanitize', config.sanitizeAll);
117+
}
118+
119+
$form.find('[data-sanitize]')
120+
.unbind('blur', execSanitationCommands)
121+
.bind('blur', execSanitationCommands);
122+
123+
});
124+
};
125+
126+
$(window).on('validatorsLoaded formValidationSetup', setupSanitation);
127+
128+
// Only for unit testing
129+
$.formUtils.setupSanitation = setupSanitation;
130+
131+
})(jQuery, window);

form-validator/sanitize.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/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.43
16+
* @version 2.2.48
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.43
16+
* @version 2.2.48
1717
*/
1818
(function($, window) {
1919

form-validator/theme-default.css

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/toggleDisabled.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @website http://formvalidator.net/
1010
* @license Dual licensed under the MIT or GPL Version 2 licenses
11-
* @version 2.2.43
11+
* @version 2.2.48
1212
*/
1313
(function($, window, undefined) {
1414

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.43
12+
* @version 2.2.48
1313
*/
1414
$.formUtils.addValidator({
1515
name : 'ukvatnumber',

0 commit comments

Comments
 (0)