Skip to content

Commit

Permalink
fix: phone number check in shared profiles
Browse files Browse the repository at this point in the history
api only accepts valid phone number or null for phone number value
  • Loading branch information
brettski committed Oct 5, 2023
1 parent 2abd368 commit 6a863f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/lib/formSchemas/sharedProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default z.object({
company: z.string().trim().nullable().optional(),
jobTitle: z.string().trim().nullable().optional(),

phone: z.string().trim().regex(PHONE_NUMBER_REGEX).optional() // todo.. this feels werid.
// THAT api cannot accept '' as phone value. Must be valid phone # or null
phone: z.string().trim().regex(PHONE_NUMBER_REGEX).or(z.literal(null)) // todo.. this feels werid.
});
40 changes: 21 additions & 19 deletions src/routes/(admin my)/my/profiles/shared/sharedProfileForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,27 @@
let countryCodeSelect = countryCode?.options?.find(({ value }) => value === $form.country);
let formattedPhoneNumber = '';
function formatPhoneNumber(event) {
// Remove all non-numeric characters from the input
$form.phone = event.target.value.replace(/\D/g, '');
// Always add a plus sign before the number
$form.phone = '+' + $form.phone;
// Format the phone number nicely
if ($form.phone.startsWith('+1')) {
// Format as a US number
formattedPhoneNumber = $form.phone.replace(
/^(\+\d{1})(\d{3})(\d{3})(\d{4})$/,
'$1 ($2) $3-$4'
);
} else {
// Format as an international number
formattedPhoneNumber = $form.phone.replace(
/^(\+\d{2})(\d{1,3})(\d{1,3})(\d{1,4})$/,
'$1 $2 $3 $4'
);
if (!$form.phone || $form.phone === '+') $form.phone = null;
if (event.target.value) {
// Remove all non-numeric characters from the input
$form.phone = event.target.value.replace(/\D/g, '');
// Always add a plus sign before the number
$form.phone = '+' + $form.phone;
// Format the phone number nicely
if ($form.phone.startsWith('+1')) {
// Format as a US number
formattedPhoneNumber = $form.phone.replace(
/^(\+\d{1})(\d{3})(\d{3})(\d{4})$/,
'$1 ($2) $3-$4'
);
} else {
// Format as an international number
formattedPhoneNumber = $form.phone.replace(
/^(\+\d{2})(\d{1,3})(\d{1,3})(\d{1,4})$/,
'$1 $2 $3 $4'
);
}
}
}
Expand Down

0 comments on commit 6a863f4

Please sign in to comment.