diff --git a/src/pages/ReimbursementAccount/ValidationStep.js b/src/pages/ReimbursementAccount/ValidationStep.js index 41f63b22f8f3..93230189b9d9 100644 --- a/src/pages/ReimbursementAccount/ValidationStep.js +++ b/src/pages/ReimbursementAccount/ValidationStep.js @@ -20,8 +20,11 @@ import TextLink from '@components/TextLink'; import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; +import * as CurrencyUtils from '@libs/CurrencyUtils'; +import getPermittedDecimalSeparator from '@libs/getPermittedDecimalSeparator'; import BankAccount from '@libs/models/BankAccount'; import * as ValidationUtils from '@libs/ValidationUtils'; +import withPolicy from '@pages/workspace/withPolicy'; import WorkspaceResetBankAccountModal from '@pages/workspace/WorkspaceResetBankAccountModal'; import * as BankAccounts from '@userActions/BankAccounts'; import * as Report from '@userActions/Report'; @@ -61,23 +64,20 @@ const defaultProps = { * Any dollar amount (e.g. 1.12) will be returned as 112 * * @param {String} amount field input + * @param {RegExp} amountRegex * @returns {String} */ -const filterInput = (amount) => { +const filterInput = (amount, amountRegex?: RegExp) => { let value = amount ? amount.toString().trim() : ''; - if (value === '' || _.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value))) { + value = value.replace(/^0+|0+$/g, ''); + if (value === '' || _.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value)) || (amountRegex && !amountRegex.test(value))) { return ''; } - // If the user enters the values in dollars, convert it to the respective cents amount - if (_.contains(value, '.')) { - value = Str.fromUSDToNumber(value); - } - return value; }; -function ValidationStep({reimbursementAccount, translate, onBackButtonPress, account, policyID}) { +function ValidationStep({reimbursementAccount, translate, onBackButtonPress, account, policyID, toLocaleDigit, policy}) { const styles = useThemeStyles(); /** * @param {Object} values - form input values passed by the Form component @@ -85,9 +85,13 @@ function ValidationStep({reimbursementAccount, translate, onBackButtonPress, acc */ const validate = (values) => { const errors = {}; + const decimalSeparator = toLocaleDigit('.'); + const outputCurrency = lodashGet(policy, 'outputCurrency', CONST.CURRENCY.USD); + + const amountRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{0,${CurrencyUtils.getCurrencyDecimals(outputCurrency)}})?$`, 'i'); _.each(values, (value, key) => { - const filteredValue = typeof value === 'string' ? filterInput(value) : value; + const filteredValue = typeof value === 'string' ? filterInput(value, amountRegex) : value; if (ValidationUtils.isRequiredFulfilled(filteredValue)) { return; } @@ -231,6 +235,7 @@ ValidationStep.displayName = 'ValidationStep'; export default compose( withLocalize, + withPolicy, withOnyx({ account: { key: ONYXKEYS.ACCOUNT,