Skip to content

Commit 2b70821

Browse files
committedMay 3, 2018
Fix float filters when using comma decimals, fixes #808
1 parent 4a1a5eb commit 2b70821

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed
 

‎lib/isFloat.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function isFloat(str, options) {
2020
if (str === '' || str === '.' || str === '-' || str === '+') {
2121
return false;
2222
}
23-
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);
23+
var value = parseFloat(str.replace(',', '.'));
24+
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);
2425
}
2526
module.exports = exports['default'];

‎src/lib/isFloat.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export default function isFloat(str, options) {
88
if (str === '' || str === '.' || str === '-' || str === '+') {
99
return false;
1010
}
11+
const value = parseFloat(str.replace(',', '.'));
1112
return float.test(str) &&
12-
(!options.hasOwnProperty('min') || str >= options.min) &&
13-
(!options.hasOwnProperty('max') || str <= options.max) &&
14-
(!options.hasOwnProperty('lt') || str < options.lt) &&
15-
(!options.hasOwnProperty('gt') || str > options.gt);
13+
(!options.hasOwnProperty('min') || value >= options.min) &&
14+
(!options.hasOwnProperty('max') || value <= options.max) &&
15+
(!options.hasOwnProperty('lt') || value < options.lt) &&
16+
(!options.hasOwnProperty('gt') || value > options.gt);
1617
}

‎test/validators.js

+32
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,38 @@ describe('Validators', function () {
21182118
'-5.5',
21192119
],
21202120
});
2121+
test({
2122+
validator: 'isFloat',
2123+
args: [{
2124+
locale: 'de-DE',
2125+
min: 3.1,
2126+
}],
2127+
valid: [
2128+
'123',
2129+
'123,',
2130+
'123,123',
2131+
'3,1',
2132+
'3,100001',
2133+
],
2134+
invalid: [
2135+
'3,09',
2136+
'-,123',
2137+
'+,123',
2138+
'01,123',
2139+
'-0,22250738585072011e-307',
2140+
'-123,123',
2141+
'-0,123',
2142+
'+0,123',
2143+
'0,123',
2144+
',0',
2145+
'123.123',
2146+
'123٫123',
2147+
' ',
2148+
'',
2149+
'.',
2150+
'foo',
2151+
],
2152+
});
21212153
});
21222154

21232155
it('should validate hexadecimal strings', function () {

‎validator.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ function isFloat(str, options) {
651651
if (str === '' || str === '.' || str === '-' || str === '+') {
652652
return false;
653653
}
654-
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);
654+
var value = parseFloat(str.replace(',', '.'));
655+
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);
655656
}
656657

657658
function decimalRegExp(options) {

‎validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.