Skip to content

Commit 26570ba

Browse files
committed
Merge pull request victorjonsson#382 from ray-print/issue323
fix for issue victorjonsson#323
2 parents d0a857e + c38fc3f commit 26570ba

27 files changed

+114
-31
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function (grunt) {
5656
}
5757
});
5858

59-
// Add options for concat ang ugligy
59+
// Add options for concat and uglify
6060
filesToBuild.concat.options = {
6161
banner: "<%= meta.banner %>"
6262
};
@@ -174,6 +174,7 @@ module.exports = function (grunt) {
174174

175175
grunt.registerTask("build", ["version", "concat", "uglify"]);
176176
grunt.registerTask('test', ['concat', 'jshint', 'qunit']);
177+
grunt.registerTask('test-uglify', ['concat', 'uglify', 'jshint', 'qunit']);
177178
grunt.registerTask("default", ["test", "build"]);
178179

179180
};

form-validator/lang/cz.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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/cz.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/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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/de.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/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.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/es.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/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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/fr.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/lang/it.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.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/it.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/lang/pl.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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/pl.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/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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/pt.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/lang/ro.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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/ro.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/lang/ru.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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

form-validator/lang/ru.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/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 MIT
9-
* @version 2.2.144
9+
* @version 2.2.145
1010
*/
1111
(function($, window) {
1212

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@
6868
},
6969
numberFormat : function(val, $input) {
7070
if ( 'numeral' in window ) {
71+
//If this has been previously formatted, it needs to be unformatted first before being reformatted.
72+
//Else numeral will fail
73+
val = numeral().unformat(val);
7174
val = numeral(val).format( $input.attr('data-sanitize-number-format') );
7275
}
7376
else {
74-
throw new Error('Using sanitation function "numberFormat" requires that you include numeraljs (http://http://numeraljs.com/)');
77+
throw new Error('Using sanitation function "numberFormat" requires that you include numeral.js ' +
78+
'(http://numeraljs.com/)');
7579
}
7680
return val;
7781
},

form-validator/src/core-validators.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@
165165
steps = $el.valAttr('step') || '',
166166
allowsSteps = false;
167167

168+
var sanitize = $el.attr('data-sanitize') || '';
169+
var isFormattedWithNumeral = sanitize.split(/\s/).find(function (name) {
170+
return (name === 'numberFormat');
171+
});
172+
if (isFormattedWithNumeral) {
173+
if (!window.numeral) {
174+
throw new ReferenceError('The data-sanitize value numberFormat cannot be used without the numeral' +
175+
' library. Please see Data Validation in http://www.formvalidator.net for more information.');
176+
}
177+
//Unformat input first, then convert back to String
178+
val = String(numeral().unformat(val));
179+
}
180+
168181
if (allowing.indexOf('number') === -1) {
169182
allowing += ',number';
170183
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"grunt-contrib-uglify": "^0.8.0",
3636
"grunt-contrib-watch": "^0.6.1",
3737
"jquery": "^2.1.4",
38+
"numeral": "~1.5.3",
3839
"qunitjs": "^1.20.0"
3940
},
4041
"dependencies": {

test/form.html

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@
265265
<div class="form-group">
266266
<label class="control-label">Number auto formatted '0,0.0000'</label>
267267
<input name="numeral" data-sanitize="numberFormat" data-sanitize-number-format="0,0.0000" />
268+
<label class="control-label">Number format with numeral</label>
269+
<input type="text" name="amount" data-validation="number" data-validation-allowing="float"
270+
data-sanitize="numberFormat" data-sanitize-number-format="0.0,0">
268271
</div>
269272
<div class="form-group">
270273
<label class="control-label">Alphanumeric (will only be validated if the checkbox is checked)</label>
@@ -425,7 +428,6 @@ <h2>Validation Module: Brazil</h2>
425428
window.applyValidation(true, '#form-a', 'top');
426429
window.applyValidation(false, '#form-b', 'element');
427430
window.applyValidation(true, '#form-c', function() {
428-
console.log('in here then');
429431
return $('#error-container');
430432
}, 'sanitize'+dev);
431433
window.applyValidation(true, '#form-d', 'element', 'html5'+dev);
@@ -450,6 +452,24 @@ <h2>Validation Module: Brazil</h2>
450452
console.log('Input '+this.name+' is '+validationResult);
451453
});
452454

455+
//Initialize numeral with the correct delimiters for the numberFormat sanitizer
456+
numeral.language('it', {
457+
delimiters: {
458+
thousands: '.',
459+
decimal: ','
460+
},
461+
abbreviations: {
462+
thousand: 'k',
463+
million: 'm',
464+
billion: 'b',
465+
trillion: 't'
466+
},
467+
currency: {
468+
symbol: '€'
469+
}
470+
});
471+
numeral.language('it');
472+
453473
})(jQuery, window);
454474
</script>
455475
</body>

test/phantom-polyfills.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//MDN
2+
if (!Array.prototype.find) {
3+
Array.prototype.find = function(predicate) {
4+
if (this === null) {
5+
throw new TypeError('Array.prototype.find called on null or undefined');
6+
}
7+
if (typeof predicate !== 'function') {
8+
throw new TypeError('predicate must be a function');
9+
}
10+
var list = Object(this);
11+
var length = list.length >>> 0;
12+
var thisArg = arguments[1];
13+
var value;
14+
15+
for (var i = 0; i < length; i++) {
16+
value = list[i];
17+
if (predicate.call(thisArg, value, i, list)) {
18+
return value;
19+
}
20+
}
21+
return undefined;
22+
};
23+
}

test/qunit.html

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
<div id="qunit-fixture"></div>
1212
<form id="dummy-form" style="visibility: hidden"></form>
1313

14+
<script src="phantom-polyfills.js"></script>
1415
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
1516
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
1617
<script src="../form-validator/jquery.form-validator.min.js"></script>
18+
<script src="../node_modules/numeral/numeral.js"></script>
1719
<script>
1820
(function($) {
1921

@@ -833,10 +835,27 @@
833835
attr: {
834836
'data-sanitize-strip' : '$, @'
835837
}
838+
}, /* This functionality doesn't work correctly. See issue #381
839+
escape: {
840+
val: '<p style="color: pink">hejsan & så\' vidare</p>',
841+
expected: '&lt;p style=&quot;color: pink&quot;&gt;hejsan &amp; så&#8217; vidare&lt;/p&gt;'
842+
}, */
843+
basicNumberFormat1: {
844+
val: '-8000.00',
845+
expected: '-8,000.0',
846+
attr: {
847+
'data-sanitize-number-format': '0,0.0'
848+
},
849+
sanitize: 'numberFormat'
836850
},
837-
escape : {
838-
val : '<p style="color: pink">hejsan & så\' vidare</p>',
839-
expected: '&lt;p style=&quot;color: pink&quot;&gt;hejsan &amp; så&#8217; vidare&lt;/p&gt;' }
851+
basicNumberFormat2: {
852+
val: '12221.444',
853+
expected: '12,221',
854+
attr: {
855+
'data-sanitize-number-format': '0,0'
856+
},
857+
sanitize: 'numberFormat'
858+
}
840859
};
841860

842861
$.each(sanitationTests, function(sanitation, data) {
@@ -846,13 +865,15 @@
846865
$input.attr(attrName, attrVal);
847866
});
848867
$.formUtils.setupSanitation({}, $form, $.formUtils.defaultConfig());
868+
849869
$input.val(data.val);
870+
871+
$input.trigger('blur');
872+
equal($input.val(), data.expected, 'Sanitize with "'+sanitation+'"');
873+
//Verify that sanitizing twice has no effect
874+
$input.trigger('focus');
850875
$input.trigger('blur');
851-
equal(
852-
$input.val(),
853-
data.expected,
854-
'Sanitize with "'+sanitation+'"'
855-
);
876+
equal($input.val(), data.expected, 'Sanitize twice with "'+sanitation+'"');
856877
});
857878

858879
});

0 commit comments

Comments
 (0)