diff --git a/packages/p2p/src/components/buy-sell/buy-sell-form.jsx b/packages/p2p/src/components/buy-sell/buy-sell-form.jsx index fa64dae229ba..2be3056b0a0a 100644 --- a/packages/p2p/src/components/buy-sell/buy-sell-form.jsx +++ b/packages/p2p/src/components/buy-sell/buy-sell-form.jsx @@ -9,6 +9,7 @@ import { localize, Localize } from 'Components/i18next'; import { useStores } from 'Stores'; import BuySellFormReceiveAmount from './buy-sell-form-receive-amount.jsx'; import PaymentMethodCard from '../my-profile/payment-methods/payment-method-card/payment-method-card.jsx'; +import { floatingPointValidator } from 'Utils/validations'; const BuySellForm = props => { const isMounted = useIsMounted(); @@ -290,6 +291,11 @@ const BuySellForm = props => { {buy_sell_store.account_currency} } + onKeyDown={event => { + if (!floatingPointValidator(event.key)) { + event.preventDefault(); + } + }} onChange={event => { if (event.target.value === '') { setFieldValue('amount', ''); diff --git a/packages/p2p/src/utils/validations.js b/packages/p2p/src/utils/validations.js index eda6728135f2..44f197422a29 100644 --- a/packages/p2p/src/utils/validations.js +++ b/packages/p2p/src/utils/validations.js @@ -3,3 +3,6 @@ export const decimalValidator = v => /^(\d+\.)?\d+$/.test(v); export const lengthValidator = v => v.length >= 1 && v.length <= 300; export const textValidator = v => /^[\p{L}\p{Nd}\s'.,:;()@#+/-]*$/u.test(v); + +// validates floating-point integers in input box that do not contain scientific notation (e, E, -, +) such as 12.2e+2 or 12.2e-2 and no negative numbers +export const floatingPointValidator = v => ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', '.'].includes(v) || /^[0-9]*[.]?[0-9]+$(?:[eE\-+]*$)/.test(v);