Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export default function isFloat(str, options) {
assertString(str);
options = options || {};
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') {
const decimalSep = options.locale ? decimal[options.locale] : '.';
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+'
|| str === '+.' || str === '-.' || str === '+,' || str === '-,'
|| str === `+${decimalSep}` || str === `-${decimalSep}`) {
Comment on lines +10 to +12

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a regular expression as suggested in the issue?

return false;
}
const value = parseFloat(str.replace(',', '.'));
Expand Down
6 changes: 6 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,8 @@ describe('Validators', () => {
'foo',
'20.foo',
'2020-01-06T14:31:00.135Z',
'+.',
'-.',
],
});

Expand Down Expand Up @@ -4711,6 +4713,8 @@ describe('Validators', () => {
'',
'.',
'foo',
'+,',
'-,',
],
});

Expand Down Expand Up @@ -4738,6 +4742,8 @@ describe('Validators', () => {
'',
'.',
'foo',
'+٫',
'-٫',
],
});

Expand Down
Loading