mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
PayoutDetailsForm: helper functions to normalize US phone numbers
This commit is contained in:
parent
cdaabd0f96
commit
2ce3be3372
1 changed files with 32 additions and 0 deletions
32
src/forms/PayoutDetailsForm/normalizePhoneNumberUS.js
Normal file
32
src/forms/PayoutDetailsForm/normalizePhoneNumberUS.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Normalize US phone number
|
||||
// Stripe requires phone numbers for US companies and their owners
|
||||
|
||||
/**
|
||||
* Format a phone number: this is just an override for default formatting.
|
||||
*/
|
||||
export const format = value => (!value ? '' : value);
|
||||
|
||||
/**
|
||||
* Parser that strips non-digit characters away from a phone number
|
||||
* string unless they are dashes in correct places.
|
||||
*
|
||||
* Returns the given US phone number in format: 202-555-0102
|
||||
*/
|
||||
export const parse = value => {
|
||||
if (!value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const phoneNumber =
|
||||
typeof value === 'string' && value.indexOf('+1') >= 0 ? value.substring(2) : value;
|
||||
|
||||
const onlyNums = phoneNumber.replace(/[^\d]/g, '');
|
||||
|
||||
if (onlyNums.length <= 3) {
|
||||
return onlyNums;
|
||||
} else if (onlyNums.length <= 7) {
|
||||
return `${onlyNums.slice(0, 3)}-${onlyNums.slice(3, 7)}`;
|
||||
} else {
|
||||
return `${onlyNums.slice(0, 3)}-${onlyNums.slice(3, 6)}-${onlyNums.slice(6, 10)}`;
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue