diff --git a/src/CONST.ts b/src/CONST.ts index a4c330b36cc6..1ad570b4444e 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1580,7 +1580,7 @@ const CONST = { APPROVE: 'approve', TRACK: 'track', }, - AMOUNT_MAX_LENGTH: 10, + AMOUNT_MAX_LENGTH: 8, RECEIPT_STATE: { SCANREADY: 'SCANREADY', OPEN: 'OPEN', diff --git a/src/libs/MoneyRequestUtils.ts b/src/libs/MoneyRequestUtils.ts index 1d55c0f49356..67ba9f62421d 100644 --- a/src/libs/MoneyRequestUtils.ts +++ b/src/libs/MoneyRequestUtils.ts @@ -37,31 +37,16 @@ function addLeadingZero(amount: string): string { return amount.startsWith('.') ? `0${amount}` : amount; } -/** - * Calculate the length of the amount with leading zeroes - */ -function calculateAmountLength(amount: string, decimals: number): number { - const leadingZeroes = amount.match(/^0+/); - const leadingZeroesLength = leadingZeroes?.[0]?.length ?? 0; - const absAmount = parseFloat((Number(stripCommaFromAmount(amount)) * 10 ** decimals).toFixed(2)).toString(); - - if (/\D/.test(absAmount)) { - return CONST.IOU.AMOUNT_MAX_LENGTH + 1; - } - - return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length); -} - /** * Check if amount is a decimal up to 3 digits */ function validateAmount(amount: string, decimals: number, amountMaxLength: number = CONST.IOU.AMOUNT_MAX_LENGTH): boolean { const regexString = decimals === 0 - ? `^\\d+(,\\d*)*$` // Don't allow decimal point if decimals === 0 - : `^\\d+(,\\d*)*(\\.\\d{0,${decimals}})?$`; // Allow the decimal point and the desired number of digits after the point + ? `^\\d{1,${amountMaxLength}}$` // Don't allow decimal point if decimals === 0 + : `^\\d{1,${amountMaxLength}}(\\.\\d{0,${decimals}})?$`; // Allow the decimal point and the desired number of digits after the point const decimalNumberRegex = new RegExp(regexString, 'i'); - return amount === '' || (decimalNumberRegex.test(amount) && calculateAmountLength(amount, decimals) <= amountMaxLength); + return amount === '' || decimalNumberRegex.test(amount); } /** diff --git a/src/pages/workspace/taxes/ValuePage.tsx b/src/pages/workspace/taxes/ValuePage.tsx index d73e5997a5ed..654618f299bd 100644 --- a/src/pages/workspace/taxes/ValuePage.tsx +++ b/src/pages/workspace/taxes/ValuePage.tsx @@ -89,7 +89,7 @@ function ValuePage({ // The default currency uses 2 decimal places, so we substract it extraDecimals={CONST.MAX_TAX_RATE_DECIMAL_PLACES - 2} // We increase the amount max length to support the extra decimals. - amountMaxLength={CONST.MAX_TAX_RATE_DECIMAL_PLACES + CONST.MAX_TAX_RATE_INTEGER_PLACES} + amountMaxLength={CONST.MAX_TAX_RATE_INTEGER_PLACES} extraSymbol={%} ref={inputCallbackRef} /> diff --git a/src/pages/workspace/taxes/WorkspaceCreateTaxPage.tsx b/src/pages/workspace/taxes/WorkspaceCreateTaxPage.tsx index d64d8f5dac39..080b92c4e454 100644 --- a/src/pages/workspace/taxes/WorkspaceCreateTaxPage.tsx +++ b/src/pages/workspace/taxes/WorkspaceCreateTaxPage.tsx @@ -108,7 +108,7 @@ function WorkspaceCreateTaxPage({ // The default currency uses 2 decimal places, so we substract it extraDecimals={CONST.MAX_TAX_RATE_DECIMAL_PLACES - 2} // We increase the amount max length to support the extra decimals. - amountMaxLength={CONST.MAX_TAX_RATE_DECIMAL_PLACES + CONST.MAX_TAX_RATE_INTEGER_PLACES} + amountMaxLength={CONST.MAX_TAX_RATE_INTEGER_PLACES} extraSymbol={%} /> diff --git a/tests/unit/MoneyRequestUtilsTest.ts b/tests/unit/MoneyRequestUtilsTest.ts new file mode 100644 index 000000000000..dfd339049d37 --- /dev/null +++ b/tests/unit/MoneyRequestUtilsTest.ts @@ -0,0 +1,22 @@ +import * as MoneyRequestUtils from '@libs/MoneyRequestUtils'; + +describe('ReportActionsUtils', () => { + describe('validateAmount', () => { + it('should pass the validation when amount is within the max digit and decimal', () => { + expect(MoneyRequestUtils.validateAmount('12345678', 2, 8)).toBe(true); + expect(MoneyRequestUtils.validateAmount('12345678', 0, 8)).toBe(true); + expect(MoneyRequestUtils.validateAmount('12345678.12', 2, 8)).toBe(true); + expect(MoneyRequestUtils.validateAmount('1234567.1', 2, 8)).toBe(true); + expect(MoneyRequestUtils.validateAmount('12345678.123', 3, 8)).toBe(true); + expect(MoneyRequestUtils.validateAmount('1234.1234', 4, 4)).toBe(true); + }); + + it("shouldn't pass the validation when amount is bigger than the max digit and decimal", () => { + expect(MoneyRequestUtils.validateAmount('12345678.123', 2, 8)).toBe(false); + expect(MoneyRequestUtils.validateAmount('12345678.1', 0, 8)).toBe(false); + expect(MoneyRequestUtils.validateAmount('123456789.12', 2, 8)).toBe(false); + expect(MoneyRequestUtils.validateAmount('123456789.1234', 3, 8)).toBe(false); + expect(MoneyRequestUtils.validateAmount('1234.12345', 4, 4)).toBe(false); + }); + }); +});