diff --git a/src/constants/countries.ts b/src/constants/countries.ts index 56ba3c1..9a6e2f9 100644 --- a/src/constants/countries.ts +++ b/src/constants/countries.ts @@ -216,6 +216,7 @@ export const isValidPhoneNumber = ( return validatePhoneNumber(val, dialCode, country) === null; }; + /** * Updates the dial code prefix of an existing phone number when country changes. * Automatically clamps remaining digits to the new country's maxLength. diff --git a/src/features/auth/components/SignUpForm.tsx b/src/features/auth/components/SignUpForm.tsx index eed6f5d..ff12ded 100644 --- a/src/features/auth/components/SignUpForm.tsx +++ b/src/features/auth/components/SignUpForm.tsx @@ -65,7 +65,7 @@ export default function SignUpForm() { const handlePhoneChange = (text: string) => { const digits = text.replace(/\D/g, ""); - setPhone(digits.slice(0, 15)); + setPhone(digits.slice(0, selectedCountry.maxLength)); if (phoneError) setPhoneError(""); }; @@ -73,8 +73,8 @@ export default function SignUpForm() { setSelectedCountry(c); setShowCountryModal(false); setCountrySearch(""); - if (phone.length > 15) { - setPhone(phone.slice(0, 15)); + if (phone.length > c.maxLength) { + setPhone(phone.slice(0, c.maxLength)); } if (phoneError) setPhoneError(""); }; @@ -101,9 +101,13 @@ export default function SignUpForm() { } if (phone.trim()) { - const digits = phone.trim().replace(/\D/g, ""); - if (digits.length < 7 || digits.length > 15) { - setPhoneError("Phone number must be between 7 and 15 digits"); + const err = validatePhoneNumber( + phone.trim(), + selectedCountry.code, + selectedCountry.country + ); + if (err) { + setPhoneError(err); hasError = true; } } @@ -231,13 +235,19 @@ export default function SignUpForm() { autoCapitalize="none" autoCorrect={false} textContentType="telephoneNumber" - maxLength={15} + maxLength={selectedCountry.maxLength} style={styles.phoneInput} /> {phoneError ? ( {phoneError} - ) : null} + ) : ( + + {selectedCountry.minLength === selectedCountry.maxLength + ? `${selectedCountry.country}: Requires exactly ${selectedCountry.maxLength} digits` + : `${selectedCountry.country}: ${selectedCountry.minLength} to ${selectedCountry.maxLength} digits`} + + )}