issue(60): enforce 7-15 digits and 15 digit input limit on phone numbers

This commit is contained in:
Urvish Prajapati 2026-10-26 16:13:39 +05:30
parent 98efd5899d
commit aebcdccc8a
4 changed files with 32 additions and 15 deletions

View File

@ -41,7 +41,8 @@ export default function LoginForm({
const handlePhoneChange = (text: string) => { const handlePhoneChange = (text: string) => {
const digits = text.replace(/\D/g, ""); const digits = text.replace(/\D/g, "");
setPhone(digits.slice(0, 10)); setPhone(digits.slice(0, 15));
if (phoneError) setPhoneError("");
}; };
const handleLogin = async () => { const handleLogin = async () => {
@ -62,8 +63,8 @@ export default function LoginForm({
if (!phone.trim()) { if (!phone.trim()) {
setPhoneError("Phone number is required"); setPhoneError("Phone number is required");
hasError = true; hasError = true;
} else if (phone.length < 10) { } else if (phone.length < 7 || phone.length > 15) {
setPhoneError("Phone number must be 10 digits"); setPhoneError("Phone number must be between 7 and 15 digits");
hasError = true; hasError = true;
} }
} }
@ -130,7 +131,7 @@ export default function LoginForm({
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
textContentType="telephoneNumber" textContentType="telephoneNumber"
maxLength={10} maxLength={15}
leftIcon="call-outline" leftIcon="call-outline"
error={phoneError} error={phoneError}
/> />

View File

@ -28,7 +28,8 @@ export default function SignUpForm() {
const handlePhoneChange = (text: string) => { const handlePhoneChange = (text: string) => {
const digits = text.replace(/\D/g, ""); const digits = text.replace(/\D/g, "");
setPhone(digits.slice(0, 10)); setPhone(digits.slice(0, 15));
if (phoneError) setPhoneError("");
}; };
const handleSignUp = async () => { const handleSignUp = async () => {
@ -52,9 +53,12 @@ export default function SignUpForm() {
hasError = true; hasError = true;
} }
if (phone.trim() && phone.length < 10) { if (phone.trim()) {
setPhoneError("Phone number must be 10 digits"); const digits = phone.trim().replace(/\D/g, "");
hasError = true; if (digits.length < 7 || digits.length > 15) {
setPhoneError("Phone number must be between 7 and 15 digits");
hasError = true;
}
} }
if (!password) { if (!password) {
@ -149,7 +153,7 @@ export default function SignUpForm() {
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
textContentType="telephoneNumber" textContentType="telephoneNumber"
maxLength={10} maxLength={15}
leftIcon="call-outline" leftIcon="call-outline"
error={phoneError} error={phoneError}
/> />

View File

@ -57,8 +57,8 @@ export const isValidPhoneNumber = (val?: string): boolean => {
return false; return false;
} }
const digits = trimmed.replace(/\D/g, ""); const digits = trimmed.replace(/\D/g, "");
// ITU-T E.164 standard allows 6 to 15 digits // ITU-T E.164 standard: minimum 7 and maximum 15 digits
return digits.length >= 6 && digits.length <= 15; return digits.length >= 7 && digits.length <= 15;
}; };
const cleanPhoneInput = (text: string): string => { const cleanPhoneInput = (text: string): string => {
@ -66,7 +66,19 @@ const cleanPhoneInput = (text: string): string => {
const hasPlus = trimmedStart.startsWith("+"); const hasPlus = trimmedStart.startsWith("+");
const rest = hasPlus ? trimmedStart.slice(1) : text; const rest = hasPlus ? trimmedStart.slice(1) : text;
const filtered = rest.replace(/[^0-9\s\-()]/g, ""); 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({ export default function EditProfileForm({
@ -126,7 +138,7 @@ export default function EditProfileForm({
const trimmedPhone = phone.trim(); const trimmedPhone = phone.trim();
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
setPhoneError("Please enter a valid phone number"); setPhoneError("Phone number must be between 7 and 15 digits");
hasError = true; hasError = true;
} }
@ -171,7 +183,7 @@ export default function EditProfileForm({
onChangeText={handlePhoneChange} onChangeText={handlePhoneChange}
placeholder="e.g. +91 9876543210" placeholder="e.g. +91 9876543210"
keyboardType="phone-pad" keyboardType="phone-pad"
maxLength={25} maxLength={16}
textContentType="telephoneNumber" textContentType="telephoneNumber"
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}

View File

@ -41,7 +41,7 @@ export default function EditProfileScreen() {
const trimmedCountry = updatedCountry.trim(); const trimmedCountry = updatedCountry.trim();
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) { 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") { if (Platform.OS === "web") {
window.alert(errMsg); window.alert(errMsg);
} else { } else {