Skip to content

Commit

Permalink
Fix float filters when using comma decimals, fixes #808
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed May 3, 2018
1 parent 4a1a5eb commit 2b70821
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function isFloat(str, options) {
if (str === '' || str === '.' || str === '-' || str === '+') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
var value = parseFloat(str.replace(',', '.'));
return float.test(str) && (!options.hasOwnProperty('min') || value >= options.min) && (!options.hasOwnProperty('max') || value <= options.max) && (!options.hasOwnProperty('lt') || value < options.lt) && (!options.hasOwnProperty('gt') || value > options.gt);
}
module.exports = exports['default'];
9 changes: 5 additions & 4 deletions src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export default function isFloat(str, options) {
if (str === '' || str === '.' || str === '-' || str === '+') {
return false;
}
const value = parseFloat(str.replace(',', '.'));
return float.test(str) &&
(!options.hasOwnProperty('min') || str >= options.min) &&
(!options.hasOwnProperty('max') || str <= options.max) &&
(!options.hasOwnProperty('lt') || str < options.lt) &&
(!options.hasOwnProperty('gt') || str > options.gt);
(!options.hasOwnProperty('min') || value >= options.min) &&
(!options.hasOwnProperty('max') || value <= options.max) &&
(!options.hasOwnProperty('lt') || value < options.lt) &&
(!options.hasOwnProperty('gt') || value > options.gt);
}
32 changes: 32 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,38 @@ describe('Validators', function () {
'-5.5',
],
});
test({
validator: 'isFloat',
args: [{
locale: 'de-DE',
min: 3.1,
}],
valid: [
'123',
'123,',
'123,123',
'3,1',
'3,100001',
],
invalid: [
'3,09',
'-,123',
'+,123',
'01,123',
'-0,22250738585072011e-307',
'-123,123',
'-0,123',
'+0,123',
'0,123',
',0',
'123.123',
'123٫123',
' ',
'',
'.',
'foo',
],
});
});

it('should validate hexadecimal strings', function () {
Expand Down
3 changes: 2 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ function isFloat(str, options) {
if (str === '' || str === '.' || str === '-' || str === '+') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
var value = parseFloat(str.replace(',', '.'));
return float.test(str) && (!options.hasOwnProperty('min') || value >= options.min) && (!options.hasOwnProperty('max') || value <= options.max) && (!options.hasOwnProperty('lt') || value < options.lt) && (!options.hasOwnProperty('gt') || value > options.gt);
}

function decimalRegExp(options) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 2b70821

Please sign in to comment.