From 082ad5cb8b96c69f9e9cc35882a1e3b6ac3712f2 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Thu, 1 Oct 2026 12:32:13 +0530 Subject: [PATCH 1/2] fix(profile): validate and restrict phone number to 10 digits (Issue #41) --- .../profile/components/EditProfileForm.tsx | 75 ++++++++++++++++--- src/screens/profile/EditProfileScreen.tsx | 20 ++++- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/src/features/profile/components/EditProfileForm.tsx b/src/features/profile/components/EditProfileForm.tsx index f646441..34ea090 100644 --- a/src/features/profile/components/EditProfileForm.tsx +++ b/src/features/profile/components/EditProfileForm.tsx @@ -56,9 +56,20 @@ export default function EditProfileForm({ onSave, isLoading = false, }: EditProfileFormProps) { + const formatPhone = (val?: string) => { + if (!val) return ""; + const digits = val.replace(/\D/g, ""); + if (digits.length === 12 && digits.startsWith("91")) { + return digits.slice(2); + } + return digits.slice(0, 10); + }; + const [fullName, setFullName] = useState(initialName); - const [phone, setPhone] = useState(initialPhone); + const [phone, setPhone] = useState(() => formatPhone(initialPhone)); const [country, setCountry] = useState(initialCountry); + const [nameError, setNameError] = useState(""); + const [phoneError, setPhoneError] = useState(""); const [showCountryModal, setShowCountryModal] = useState(false); useEffect(() => { @@ -66,23 +77,63 @@ export default function EditProfileForm({ }, [initialName]); useEffect(() => { - setPhone(initialPhone); + setPhone(formatPhone(initialPhone)); }, [initialPhone]); useEffect(() => { setCountry(initialCountry); }, [initialCountry]); + const handleNameChange = (text: string) => { + setFullName(text); + if (nameError && text.trim()) { + setNameError(""); + } + }; + + const handlePhoneChange = (text: string) => { + const cleaned = formatPhone(text); + setPhone(cleaned); + if (phoneError) { + if (!cleaned || cleaned.length === 10) { + setPhoneError(""); + } + } + }; + + const handleSave = () => { + let hasError = false; + setNameError(""); + setPhoneError(""); + + const trimmedName = fullName.trim(); + if (!trimmedName) { + setNameError("Full name is required"); + hasError = true; + } + + const trimmedPhone = phone.trim(); + if (trimmedPhone && trimmedPhone.length < 10) { + setPhoneError("Phone number must be 10 digits"); + hasError = true; + } + + if (hasError) return; + + onSave?.(trimmedName, trimmedPhone, country); + }; + return ( @@ -105,12 +156,20 @@ export default function EditProfileForm({ + {!phoneError && ( + + Enter 10-digit mobile number for login and notifications. + + )} @@ -126,9 +185,7 @@ export default function EditProfileForm({ { - onSave?.(fullName, phone, country); - }} + onPress={handleSave} /> diff --git a/src/screens/profile/EditProfileScreen.tsx b/src/screens/profile/EditProfileScreen.tsx index 3b8406d..7fedc6b 100644 --- a/src/screens/profile/EditProfileScreen.tsx +++ b/src/screens/profile/EditProfileScreen.tsx @@ -36,12 +36,26 @@ export default function EditProfileScreen() { return; } + const trimmedName = updatedName.trim(); + const trimmedPhone = updatedPhone.trim(); + const trimmedCountry = updatedCountry.trim(); + + if (trimmedPhone && trimmedPhone.length < 10) { + const errMsg = "Phone number must be 10 digits"; + if (Platform.OS === "web") { + window.alert(errMsg); + } else { + Alert.alert("Validation Error", errMsg); + } + return; + } + setSaving(true); try { await apiClient.put("/auth/profile", { - full_name: updatedName.trim(), - phone_number: updatedPhone.trim() || null, - country: updatedCountry.trim() || null, + full_name: trimmedName, + phone_number: trimmedPhone || null, + country: trimmedCountry || null, }); await refreshIdentity(); -- 2.34.1 From 2d05158fc2135d28627f7fb9610558f3db0db5eb Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Sat, 3 Oct 2026 10:54:03 +0530 Subject: [PATCH 2/2] fix(profile): allow variable length international phone numbers (Issue #41) --- .../profile/components/EditProfileForm.tsx | 50 ++++++++++++------- src/screens/profile/EditProfileScreen.tsx | 6 +-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/features/profile/components/EditProfileForm.tsx b/src/features/profile/components/EditProfileForm.tsx index 34ea090..586e242 100644 --- a/src/features/profile/components/EditProfileForm.tsx +++ b/src/features/profile/components/EditProfileForm.tsx @@ -48,6 +48,27 @@ const COMMON_COUNTRIES = [ "Spain", ]; +export const isValidPhoneNumber = (val?: 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 allows 6 to 15 digits + return digits.length >= 6 && digits.length <= 15; +}; + +const cleanPhoneInput = (text: string): string => { + const trimmedStart = text.trimStart(); + const hasPlus = trimmedStart.startsWith("+"); + const rest = hasPlus ? trimmedStart.slice(1) : text; + const filtered = rest.replace(/[^0-9\s\-()]/g, ""); + return hasPlus ? `+${filtered}` : filtered; +}; + export default function EditProfileForm({ initialName = "User Name", initialEmail = "user@example.com", @@ -56,17 +77,8 @@ export default function EditProfileForm({ onSave, isLoading = false, }: EditProfileFormProps) { - const formatPhone = (val?: string) => { - if (!val) return ""; - const digits = val.replace(/\D/g, ""); - if (digits.length === 12 && digits.startsWith("91")) { - return digits.slice(2); - } - return digits.slice(0, 10); - }; - const [fullName, setFullName] = useState(initialName); - const [phone, setPhone] = useState(() => formatPhone(initialPhone)); + const [phone, setPhone] = useState(initialPhone || ""); const [country, setCountry] = useState(initialCountry); const [nameError, setNameError] = useState(""); const [phoneError, setPhoneError] = useState(""); @@ -77,7 +89,7 @@ export default function EditProfileForm({ }, [initialName]); useEffect(() => { - setPhone(formatPhone(initialPhone)); + setPhone(initialPhone || ""); }, [initialPhone]); useEffect(() => { @@ -92,10 +104,10 @@ export default function EditProfileForm({ }; const handlePhoneChange = (text: string) => { - const cleaned = formatPhone(text); + const cleaned = cleanPhoneInput(text); setPhone(cleaned); if (phoneError) { - if (!cleaned || cleaned.length === 10) { + if (!cleaned.trim() || isValidPhoneNumber(cleaned)) { setPhoneError(""); } } @@ -113,8 +125,8 @@ export default function EditProfileForm({ } const trimmedPhone = phone.trim(); - if (trimmedPhone && trimmedPhone.length < 10) { - setPhoneError("Phone number must be 10 digits"); + if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { + setPhoneError("Please enter a valid phone number"); hasError = true; } @@ -157,9 +169,9 @@ export default function EditProfileForm({ {!phoneError && ( - Enter 10-digit mobile number for login and notifications. + Enter mobile number with country code for login and notifications. )} diff --git a/src/screens/profile/EditProfileScreen.tsx b/src/screens/profile/EditProfileScreen.tsx index 7fedc6b..0a3d349 100644 --- a/src/screens/profile/EditProfileScreen.tsx +++ b/src/screens/profile/EditProfileScreen.tsx @@ -16,7 +16,7 @@ import { router } from "expo-router"; import SettingsHeader from "@/features/settings/components/SettingsHeader"; import { Colors } from "@/theme"; -import EditProfileForm from "@/features/profile/components/EditProfileForm"; +import EditProfileForm, { isValidPhoneNumber } from "@/features/profile/components/EditProfileForm"; import EditProfileAvatar from "@/features/profile/components/EditProfileAvatar"; import { useAuthStore } from "@/store/auth.store"; import { apiClient } from "@/api"; @@ -40,8 +40,8 @@ export default function EditProfileScreen() { const trimmedPhone = updatedPhone.trim(); const trimmedCountry = updatedCountry.trim(); - if (trimmedPhone && trimmedPhone.length < 10) { - const errMsg = "Phone number must be 10 digits"; + if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { + const errMsg = "Please enter a valid phone number"; if (Platform.OS === "web") { window.alert(errMsg); } else { -- 2.34.1