Skip to content

Commit 7da3b98

Browse files
committed
Fix IE compatibility issue, numeral errors on empty strings
-Array.prototype.find not supported yet in IE (switch to using regex) -Check for empty string before calling numeral functions -Avoid testing on alternate delimiters in numeral -- numeral does not seem to be returning correct values
1 parent e9ec005 commit 7da3b98

File tree

5 files changed

+15
-50
lines changed

5 files changed

+15
-50
lines changed

form-validator/sanitize.dev.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@
6767
return this.insert(val, $input, 'left');
6868
},
6969
numberFormat : function(val, $input) {
70+
if (val.length === 0) {
71+
return val;
72+
}
7073
if ( 'numeral' in window ) {
7174
//If this has been previously formatted, it needs to be unformatted first before being reformatted.
7275
//Else numeral will fail
7376
val = numeral().unformat(val);
7477
val = numeral(val).format( $input.attr('data-sanitize-number-format') );
7578
}
7679
else {
77-
throw new Error('Using sanitation function "numberFormat" requires that you include numeral.js ' +
80+
throw new ReferenceError('Using sanitation function "numberFormat" requires that you include numeral.js ' +
7881
'(http://numeraljs.com/)');
7982
}
8083
return val;

form-validator/src/core-validators.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,16 @@
166166
allowsSteps = false;
167167

168168
var sanitize = $el.attr('data-sanitize') || '';
169-
var isFormattedWithNumeral = sanitize.split(/\s/).find(function (name) {
170-
return (name === 'numberFormat');
171-
});
169+
var isFormattedWithNumeral = sanitize.match(/(^|[\s])numberFormat([\s]|$)/i);
172170
if (isFormattedWithNumeral) {
173171
if (!window.numeral) {
174172
throw new ReferenceError('The data-sanitize value numberFormat cannot be used without the numeral' +
175173
' library. Please see Data Validation in http://www.formvalidator.net for more information.');
176174
}
177175
//Unformat input first, then convert back to String
178-
val = String(numeral().unformat(val));
176+
if (val.length) {
177+
val = String(numeral().unformat(val));
178+
}
179179
}
180180

181181
if (allowing.indexOf('number') === -1) {

test/form.html

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,16 @@
264264
<label class="control-label">Confirm e-mail</label>
265265
<input name="test" data-validation="confirmation" data-validation-confirm="testmail" />
266266
</div>
267-
<div class="form-group">
267+
<div class="form-group">
268+
<!-- The numeral library appears to not parse correctly when periods are the thousands delimiter
269+
and commas are the decimal delimiter. -->
268270
<label class="control-label">Number auto formatted '0,0.0000'</label>
269271
<input name="numeral" data-sanitize="numberFormat" data-sanitize-number-format="0,0.0000" />
270-
<label class="control-label">Number format with numeral</label>
272+
</div>
273+
<div class="form-group">
274+
<label class="control-label">Number format '0,0.0'</label>
271275
<input type="text" name="amount" data-validation="number" data-validation-allowing="float"
272-
data-sanitize="numberFormat" data-sanitize-number-format="0.0,0">
276+
data-sanitize="numberFormat" data-sanitize-number-format="0,0.0">
273277
</div>
274278
<div class="form-group">
275279
<label class="control-label">Alphanumeric (will only be validated if the checkbox is checked)</label>
@@ -454,24 +458,6 @@ <h2>Validation Module: Brazil</h2>
454458
console.log('Input '+this.name+' is '+validationResult);
455459
});
456460

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

test/phantom-polyfills.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/qunit.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<div id="qunit-fixture"></div>
1212
<form id="dummy-form" style="visibility: hidden"></form>
1313

14-
<script src="phantom-polyfills.js"></script>
1514
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
1615
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
1716
<script src="../form-validator/jquery.form-validator.min.js"></script>

0 commit comments

Comments
 (0)