Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adrienne/Fixed floating-point notations on buy/sell ads input amount #5168

6 changes: 6 additions & 0 deletions packages/p2p/src/components/buy-sell/buy-sell-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -290,6 +291,11 @@ const BuySellForm = props => {
{buy_sell_store.account_currency}
</Text>
}
onKeyDown={event => {
if (!floatingPointValidator(event.key)) {
event.preventDefault();
}
}}
onChange={event => {
if (event.target.value === '') {
setFieldValue('amount', '');
Expand Down
3 changes: 3 additions & 0 deletions packages/p2p/src/utils/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);