From aebcdccc8a5fa94a63511092fafd24733685e0fd Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 26 Oct 2026 16:13:39 +0530 Subject: [PATCH] issue(60): enforce 7-15 digits and 15 digit input limit on phone numbers --- src/features/auth/components/LoginForm.tsx | 9 ++++---- src/features/auth/components/SignUpForm.tsx | 14 +++++++----- .../profile/components/EditProfileForm.tsx | 22 ++++++++++++++----- src/screens/profile/EditProfileScreen.tsx | 2 +- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/features/auth/components/LoginForm.tsx b/src/features/auth/components/LoginForm.tsx index 77d73af..1ba7308 100644 --- a/src/features/auth/components/LoginForm.tsx +++ b/src/features/auth/components/LoginForm.tsx @@ -41,7 +41,8 @@ export default function LoginForm({ const handlePhoneChange = (text: string) => { const digits = text.replace(/\D/g, ""); - setPhone(digits.slice(0, 10)); + setPhone(digits.slice(0, 15)); + if (phoneError) setPhoneError(""); }; const handleLogin = async () => { @@ -62,8 +63,8 @@ export default function LoginForm({ if (!phone.trim()) { setPhoneError("Phone number is required"); hasError = true; - } else if (phone.length < 10) { - setPhoneError("Phone number must be 10 digits"); + } else if (phone.length < 7 || phone.length > 15) { + setPhoneError("Phone number must be between 7 and 15 digits"); hasError = true; } } @@ -130,7 +131,7 @@ export default function LoginForm({ autoCapitalize="none" autoCorrect={false} textContentType="telephoneNumber" - maxLength={10} + maxLength={15} leftIcon="call-outline" error={phoneError} /> diff --git a/src/features/auth/components/SignUpForm.tsx b/src/features/auth/components/SignUpForm.tsx index bc428c0..f83e5d8 100644 --- a/src/features/auth/components/SignUpForm.tsx +++ b/src/features/auth/components/SignUpForm.tsx @@ -28,7 +28,8 @@ export default function SignUpForm() { const handlePhoneChange = (text: string) => { const digits = text.replace(/\D/g, ""); - setPhone(digits.slice(0, 10)); + setPhone(digits.slice(0, 15)); + if (phoneError) setPhoneError(""); }; const handleSignUp = async () => { @@ -52,9 +53,12 @@ export default function SignUpForm() { hasError = true; } - if (phone.trim() && phone.length < 10) { - setPhoneError("Phone number must be 10 digits"); - hasError = true; + 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"); + hasError = true; + } } if (!password) { @@ -149,7 +153,7 @@ export default function SignUpForm() { autoCapitalize="none" autoCorrect={false} textContentType="telephoneNumber" - maxLength={10} + maxLength={15} leftIcon="call-outline" error={phoneError} /> diff --git a/src/features/profile/components/EditProfileForm.tsx b/src/features/profile/components/EditProfileForm.tsx index 586e242..5ceeb7b 100644 --- a/src/features/profile/components/EditProfileForm.tsx +++ b/src/features/profile/components/EditProfileForm.tsx @@ -57,8 +57,8 @@ export const isValidPhoneNumber = (val?: string): boolean => { 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; + // ITU-T E.164 standard: minimum 7 and maximum 15 digits + return digits.length >= 7 && digits.length <= 15; }; const cleanPhoneInput = (text: string): string => { @@ -66,7 +66,19 @@ const cleanPhoneInput = (text: string): string => { const hasPlus = trimmedStart.startsWith("+"); const rest = hasPlus ? trimmedStart.slice(1) : text; const filtered = rest.replace(/[^0-9\s\-()]/g, ""); - return hasPlus ? `+${filtered}` : filtered; + let digitCount = 0; + let bounded = ""; + for (const ch of filtered) { + if (/\d/.test(ch)) { + if (digitCount < 15) { + digitCount++; + bounded += ch; + } + } else { + bounded += ch; + } + } + return hasPlus ? `+${bounded}` : bounded; }; export default function EditProfileForm({ @@ -126,7 +138,7 @@ export default function EditProfileForm({ const trimmedPhone = phone.trim(); if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { - setPhoneError("Please enter a valid phone number"); + setPhoneError("Phone number must be between 7 and 15 digits"); hasError = true; } @@ -171,7 +183,7 @@ export default function EditProfileForm({ onChangeText={handlePhoneChange} placeholder="e.g. +91 9876543210" keyboardType="phone-pad" - maxLength={25} + maxLength={16} textContentType="telephoneNumber" autoCapitalize="none" autoCorrect={false} diff --git a/src/screens/profile/EditProfileScreen.tsx b/src/screens/profile/EditProfileScreen.tsx index 0a3d349..0ef223f 100644 --- a/src/screens/profile/EditProfileScreen.tsx +++ b/src/screens/profile/EditProfileScreen.tsx @@ -41,7 +41,7 @@ export default function EditProfileScreen() { const trimmedCountry = updatedCountry.trim(); if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { - const errMsg = "Please enter a valid phone number"; + const errMsg = "Phone number must be between 7 and 15 digits"; if (Platform.OS === "web") { window.alert(errMsg); } else {