Skip to content

Commit

Permalink
Merge pull request #44400 from nkdengineer/fix/44111
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Jun 26, 2024
2 parents aecc38b + bc8dfd4 commit 67186f1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/AddPaymentCard/PaymentCardForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function PaymentCardForm({

const [isCurrencyModalVisible, setIsCurrencyModalVisible] = useState(false);
const [currency, setCurrency] = useState<keyof typeof CONST.CURRENCY>(CONST.CURRENCY.USD);
const [cardNumber, setCardNumber] = useState('');

const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.ADD_DEBIT_CARD_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.ADD_DEBIT_CARD_FORM> => {
const errors = ValidationUtils.getFieldRequiredErrors(values, REQUIRED_FIELDS);
Expand Down Expand Up @@ -181,6 +182,23 @@ function PaymentCardForm({
setIsCurrencyModalVisible(false);
}, []);

const onChangeCardNumber = useCallback((newValue: string) => {
// replace all characters that are not spaces or digits
let validCardNumber = newValue.replace(/[^\d ]/g, '');

// gets only the first 16 digits if the inputted number have more digits than that
validCardNumber = validCardNumber.match(/(?:\d *){1,16}/)?.[0] ?? '';

// add the spacing between every 4 digits
validCardNumber =
validCardNumber
.replace(/ /g, '')
.match(/.{1,4}/g)
?.join(' ') ?? '';

setCardNumber(validCardNumber);
}, []);

if (!shouldShowPaymentCardForm) {
return null;
}
Expand All @@ -204,6 +222,8 @@ function PaymentCardForm({
role={CONST.ROLE.PRESENTATION}
ref={cardNumberRef}
inputMode={CONST.INPUT_MODE.NUMERIC}
onChangeText={onChangeCardNumber}
value={cardNumber}
/>
<InputWrapper
InputComponent={TextInput}
Expand Down

0 comments on commit 67186f1

Please sign in to comment.