diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index 0dfbe77fcf..499957f6e5 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -79,7 +79,11 @@ export function integer(i18n) { export function number(i18n) { return value => { const str = String(value); - if (/^[0-9]*(\.[0-9]*)?$/.test(str)) { + if (/^-?[0-9]*(\.[0-9]*)?$/.test(str)) { + return undefined; + } + // large number scientific notation (e.g. '1e+21') + if (/^-?[0-9]*e[+-][0-9]*$/.test(str)) { return undefined; } return i18n._(t`This field must be a number`); diff --git a/awx/ui_next/src/util/validators.test.js b/awx/ui_next/src/util/validators.test.js index 6a0a221d57..a660c8ea5c 100644 --- a/awx/ui_next/src/util/validators.test.js +++ b/awx/ui_next/src/util/validators.test.js @@ -121,10 +121,19 @@ describe('validators', () => { expect(number(i18n)('13')).toBeUndefined(); }); + test('number should accept negative number', () => { + expect(number(i18n)(-14)).toBeUndefined(); + }); + test('number should accept decimal/float', () => { expect(number(i18n)(13.1)).toBeUndefined(); }); + test('number should accept large number', () => { + expect(number(i18n)(999999999999999999999.9)).toBeUndefined(); + expect(number(i18n)(-999999999999999999999.9)).toBeUndefined(); + }); + test('number should reject string containing alphanum', () => { expect(number(i18n)('15a')).toEqual({ id: 'This field must be a number',