diff --git a/src/constants/countries.ts b/src/constants/countries.ts index 7fb3350..d86294d 100644 --- a/src/constants/countries.ts +++ b/src/constants/countries.ts @@ -105,7 +105,7 @@ export const getCountryRuleByDialCode = (dialCode: string): CountryCode => { label: dialCode, country: "Other", currency: "USD", - minLength: 6, + minLength: 7, maxLength: 15, placeholder: "Phone number", } @@ -125,7 +125,7 @@ export const getCountryRuleByName = (countryName: string): CountryCode => { label: "🇮🇳 +91", country: countryName, currency: normalized === "india" ? "INR" : "USD", - minLength: 6, + minLength: 7, maxLength: 15, placeholder: "Phone number", } @@ -140,10 +140,16 @@ export const validatePhoneNumber = ( if (!val || !val.trim()) return null; const trimmed = val.trim(); + // Explicitly disallow alphabetic characters if (/[a-zA-Z]/.test(trimmed)) { return "Phone number cannot contain alphabetic characters."; } + // Strictly enforce numeric digits with optional leading '+' and standard formatting separators + if (!/^\+?[\d\s\-()]+$/.test(trimmed)) { + return "Please enter a valid phone number containing only numeric digits."; + } + const allDigits = trimmed.replace(/\D/g, ""); if (allDigits.length === 0) { return "Please enter a valid phone number with numeric digits."; @@ -160,26 +166,47 @@ export const validatePhoneNumber = ( } if (rule && rule.country !== "Other") { + const dialDigits = rule.code.replace(/\D/g, ""); + let localDigits = allDigits; + + // If formatted with country code (e.g. +919876543210 or 919876543210 where total length > rule.maxLength), strip country prefix + if ( + (trimmed.startsWith("+") || allDigits.length > rule.maxLength) && + allDigits.startsWith(dialDigits) && + allDigits.length === dialDigits.length + rule.minLength + ) { + localDigits = allDigits.slice(dialDigits.length); + } + if (rule.minLength === rule.maxLength) { - if (allDigits.length !== rule.minLength) { + if (localDigits.length !== rule.minLength) { return `${rule.country} phone number must be exactly ${rule.minLength} digits.`; } } else { - if (allDigits.length < rule.minLength) { + if (localDigits.length < rule.minLength) { return `${rule.country} phone number must be at least ${rule.minLength} digits.`; } - if (allDigits.length > rule.maxLength) { + if (localDigits.length > rule.maxLength) { return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`; } } return null; } - if (allDigits.length < 6) { - return "Phone number must contain at least 6 digits."; + // General fallback ITU E.164 + if (allDigits.length < 7) { + return "Phone number must contain at least 7 digits."; } if (allDigits.length > 15) { return "Phone number cannot exceed 15 digits."; } return null; }; + +export const isValidPhoneNumber = ( + val?: string | null, + dialCode?: string, + country?: string +): boolean => { + return validatePhoneNumber(val, dialCode, country) === null; +}; 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`} + + )} { +export const isValidPhoneNumber = (val?: string, country?: string): boolean => { if (!val || !val.trim()) return true; - const trimmed = val.trim(); - // Must match valid phone character format (optional '+', digits, spaces, hyphens, parentheses) - const phoneRegex = /^\+?[0-9\s\-()]+$/; - if (!phoneRegex.test(trimmed)) { - return false; - } - const digits = trimmed.replace(/\D/g, ""); - // ITU-T E.164 standard: minimum 7 and maximum 15 digits - return digits.length >= 7 && digits.length <= 15; + return validatePhoneNumber(val, undefined, country) === null; }; const cleanPhoneInput = (text: string): string => { @@ -119,7 +112,7 @@ export default function EditProfileForm({ const cleaned = cleanPhoneInput(text); setPhone(cleaned); if (phoneError) { - if (!cleaned.trim() || isValidPhoneNumber(cleaned)) { + if (!cleaned.trim() || isValidPhoneNumber(cleaned, country)) { setPhoneError(""); } } @@ -137,9 +130,12 @@ export default function EditProfileForm({ } const trimmedPhone = phone.trim(); - if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { - setPhoneError("Phone number must be between 7 and 15 digits"); - hasError = true; + if (trimmedPhone) { + const err = validatePhoneNumber(trimmedPhone, undefined, country); + if (err) { + setPhoneError(err); + hasError = true; + } } if (hasError) return; diff --git a/src/screens/profile/EditProfileScreen.tsx b/src/screens/profile/EditProfileScreen.tsx index 0ef223f..ca1ab9e 100644 --- a/src/screens/profile/EditProfileScreen.tsx +++ b/src/screens/profile/EditProfileScreen.tsx @@ -16,10 +16,11 @@ import { router } from "expo-router"; import SettingsHeader from "@/features/settings/components/SettingsHeader"; import { Colors } from "@/theme"; -import EditProfileForm, { isValidPhoneNumber } from "@/features/profile/components/EditProfileForm"; +import EditProfileForm from "@/features/profile/components/EditProfileForm"; import EditProfileAvatar from "@/features/profile/components/EditProfileAvatar"; import { useAuthStore } from "@/store/auth.store"; import { apiClient } from "@/api"; +import { validatePhoneNumber } from "@/constants/countries"; export default function EditProfileScreen() { const user = useAuthStore((state) => state.user); @@ -40,14 +41,16 @@ export default function EditProfileScreen() { const trimmedPhone = updatedPhone.trim(); const trimmedCountry = updatedCountry.trim(); - if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { - const errMsg = "Phone number must be between 7 and 15 digits"; - if (Platform.OS === "web") { - window.alert(errMsg); - } else { - Alert.alert("Validation Error", errMsg); + if (trimmedPhone) { + const errMsg = validatePhoneNumber(trimmedPhone, undefined, trimmedCountry); + if (errMsg) { + if (Platform.OS === "web") { + window.alert(errMsg); + } else { + Alert.alert("Validation Error", errMsg); + } + return; } - return; } setSaving(true);