diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index 84bdc782c..5eeb9f511 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -5,7 +5,10 @@ import { decimal } from './alpha'; 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]+))?$`); + // Fall back to `.` when the locale is unknown; otherwise `decimal[locale]` + // is `undefined` and stringifies into the pattern as the literal `undefined`. + const decimalSeparator = decimal[options.locale] || '.'; + const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${decimalSeparator}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`); if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') { return false; } diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..035611dc6 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4857,6 +4857,23 @@ describe('Validators', () => { 'foo', ], }); + test({ + validator: 'isFloat', + args: [{ + locale: 'is-NOT-a-locale', + }], + valid: [ + '123', + '123.123', + '-3.5e2', + ], + invalid: [ + '3undefined5', + '123,123', + 'foo', + '', + ], + }); test({ validator: 'isFloat', args: [{