bug(issue-82):fixed country wise mobile number length issue #95
|
|
@ -105,7 +105,7 @@ export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
||||||
label: dialCode,
|
label: dialCode,
|
||||||
country: "Other",
|
country: "Other",
|
||||||
currency: "USD",
|
currency: "USD",
|
||||||
minLength: 6,
|
minLength: 7,
|
||||||
maxLength: 15,
|
maxLength: 15,
|
||||||
placeholder: "Phone number",
|
placeholder: "Phone number",
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +125,7 @@ export const getCountryRuleByName = (countryName: string): CountryCode => {
|
||||||
label: "🇮🇳 +91",
|
label: "🇮🇳 +91",
|
||||||
country: countryName,
|
country: countryName,
|
||||||
currency: normalized === "india" ? "INR" : "USD",
|
currency: normalized === "india" ? "INR" : "USD",
|
||||||
minLength: 6,
|
minLength: 7,
|
||||||
maxLength: 15,
|
maxLength: 15,
|
||||||
placeholder: "Phone number",
|
placeholder: "Phone number",
|
||||||
}
|
}
|
||||||
|
|
@ -140,10 +140,16 @@ export const validatePhoneNumber = (
|
||||||
if (!val || !val.trim()) return null;
|
if (!val || !val.trim()) return null;
|
||||||
const trimmed = val.trim();
|
const trimmed = val.trim();
|
||||||
|
|
||||||
|
// Explicitly disallow alphabetic characters
|
||||||
if (/[a-zA-Z]/.test(trimmed)) {
|
if (/[a-zA-Z]/.test(trimmed)) {
|
||||||
return "Phone number cannot contain alphabetic characters.";
|
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, "");
|
const allDigits = trimmed.replace(/\D/g, "");
|
||||||
if (allDigits.length === 0) {
|
if (allDigits.length === 0) {
|
||||||
return "Please enter a valid phone number with numeric digits.";
|
return "Please enter a valid phone number with numeric digits.";
|
||||||
|
|
@ -160,26 +166,47 @@ export const validatePhoneNumber = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rule && rule.country !== "Other") {
|
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 (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.`;
|
return `${rule.country} phone number must be exactly ${rule.minLength} digits.`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (allDigits.length < rule.minLength) {
|
if (localDigits.length < rule.minLength) {
|
||||||
return `${rule.country} phone number must be at least ${rule.minLength} digits.`;
|
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 `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allDigits.length < 6) {
|
// General fallback ITU E.164
|
||||||
return "Phone number must contain at least 6 digits.";
|
if (allDigits.length < 7) {
|
||||||
|
return "Phone number must contain at least 7 digits.";
|
||||||
}
|
}
|
||||||
if (allDigits.length > 15) {
|
if (allDigits.length > 15) {
|
||||||
return "Phone number cannot exceed 15 digits.";
|
return "Phone number cannot exceed 15 digits.";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isValidPhoneNumber = (
|
||||||
|
val?: string | null,
|
||||||
|
dialCode?: string,
|
||||||
|
country?: string
|
||||||
|
): boolean => {
|
||||||
|
return validatePhoneNumber(val, dialCode, country) === null;
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ 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, 15));
|
setPhone(digits.slice(0, selectedCountry.maxLength));
|
||||||
if (phoneError) setPhoneError("");
|
if (phoneError) setPhoneError("");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -73,8 +73,8 @@ export default function SignUpForm() {
|
||||||
setSelectedCountry(c);
|
setSelectedCountry(c);
|
||||||
setShowCountryModal(false);
|
setShowCountryModal(false);
|
||||||
setCountrySearch("");
|
setCountrySearch("");
|
||||||
if (phone.length > 15) {
|
if (phone.length > c.maxLength) {
|
||||||
setPhone(phone.slice(0, 15));
|
setPhone(phone.slice(0, c.maxLength));
|
||||||
}
|
}
|
||||||
if (phoneError) setPhoneError("");
|
if (phoneError) setPhoneError("");
|
||||||
};
|
};
|
||||||
|
|
@ -101,9 +101,13 @@ export default function SignUpForm() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phone.trim()) {
|
if (phone.trim()) {
|
||||||
const digits = phone.trim().replace(/\D/g, "");
|
const err = validatePhoneNumber(
|
||||||
if (digits.length < 7 || digits.length > 15) {
|
phone.trim(),
|
||||||
setPhoneError("Phone number must be between 7 and 15 digits");
|
selectedCountry.code,
|
||||||
|
selectedCountry.country
|
||||||
|
);
|
||||||
|
if (err) {
|
||||||
|
setPhoneError(err);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -231,13 +235,19 @@ export default function SignUpForm() {
|
||||||
autoCapitalize="none"
|
autoCapitalize="none"
|
||||||
autoCorrect={false}
|
autoCorrect={false}
|
||||||
textContentType="telephoneNumber"
|
textContentType="telephoneNumber"
|
||||||
maxLength={15}
|
maxLength={selectedCountry.maxLength}
|
||||||
style={styles.phoneInput}
|
style={styles.phoneInput}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
{phoneError ? (
|
{phoneError ? (
|
||||||
<Text style={styles.errorText}>{phoneError}</Text>
|
<Text style={styles.errorText}>{phoneError}</Text>
|
||||||
) : null}
|
) : (
|
||||||
|
<Text style={styles.phoneHelperText}>
|
||||||
|
{selectedCountry.minLength === selectedCountry.maxLength
|
||||||
|
? `${selectedCountry.country}: Requires exactly ${selectedCountry.maxLength} digits`
|
||||||
|
: `${selectedCountry.country}: ${selectedCountry.minLength} to ${selectedCountry.maxLength} digits`}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<PasswordInput
|
<PasswordInput
|
||||||
|
|
@ -437,6 +447,12 @@ const styles = StyleSheet.create({
|
||||||
marginTop: verticalScale(6),
|
marginTop: verticalScale(6),
|
||||||
marginLeft: scale(6),
|
marginLeft: scale(6),
|
||||||
},
|
},
|
||||||
|
phoneHelperText: {
|
||||||
|
color: AuthColors.textSecondary,
|
||||||
|
fontSize: moderateScale(11),
|
||||||
|
marginTop: verticalScale(4),
|
||||||
|
marginLeft: scale(6),
|
||||||
|
},
|
||||||
|
|
||||||
modalOverlay: {
|
modalOverlay: {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import DropdownField from "@/components/forms/DropdownField";
|
||||||
import FieldLabel from "@/components/forms/FieldLabel";
|
import FieldLabel from "@/components/forms/FieldLabel";
|
||||||
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
||||||
import { Colors } from "@/theme";
|
import { Colors } from "@/theme";
|
||||||
|
import { validatePhoneNumber } from "@/constants/countries";
|
||||||
|
|
||||||
interface EditProfileFormProps {
|
interface EditProfileFormProps {
|
||||||
initialName?: string;
|
initialName?: string;
|
||||||
|
|
@ -48,17 +49,9 @@ const COMMON_COUNTRIES = [
|
||||||
"Spain",
|
"Spain",
|
||||||
];
|
];
|
||||||
|
|
||||||
export const isValidPhoneNumber = (val?: string): boolean => {
|
export const isValidPhoneNumber = (val?: string, country?: string): boolean => {
|
||||||
if (!val || !val.trim()) return true;
|
if (!val || !val.trim()) return true;
|
||||||
const trimmed = val.trim();
|
return validatePhoneNumber(val, undefined, country) === null;
|
||||||
// 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const cleanPhoneInput = (text: string): string => {
|
const cleanPhoneInput = (text: string): string => {
|
||||||
|
|
@ -119,7 +112,7 @@ export default function EditProfileForm({
|
||||||
const cleaned = cleanPhoneInput(text);
|
const cleaned = cleanPhoneInput(text);
|
||||||
setPhone(cleaned);
|
setPhone(cleaned);
|
||||||
if (phoneError) {
|
if (phoneError) {
|
||||||
if (!cleaned.trim() || isValidPhoneNumber(cleaned)) {
|
if (!cleaned.trim() || isValidPhoneNumber(cleaned, country)) {
|
||||||
setPhoneError("");
|
setPhoneError("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -137,10 +130,13 @@ export default function EditProfileForm({
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimmedPhone = phone.trim();
|
const trimmedPhone = phone.trim();
|
||||||
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
|
if (trimmedPhone) {
|
||||||
setPhoneError("Phone number must be between 7 and 15 digits");
|
const err = validatePhoneNumber(trimmedPhone, undefined, country);
|
||||||
|
if (err) {
|
||||||
|
setPhoneError(err);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (hasError) return;
|
if (hasError) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,11 @@ import { router } from "expo-router";
|
||||||
|
|
||||||
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
||||||
import { Colors } from "@/theme";
|
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 EditProfileAvatar from "@/features/profile/components/EditProfileAvatar";
|
||||||
import { useAuthStore } from "@/store/auth.store";
|
import { useAuthStore } from "@/store/auth.store";
|
||||||
import { apiClient } from "@/api";
|
import { apiClient } from "@/api";
|
||||||
|
import { validatePhoneNumber } from "@/constants/countries";
|
||||||
|
|
||||||
export default function EditProfileScreen() {
|
export default function EditProfileScreen() {
|
||||||
const user = useAuthStore((state) => state.user);
|
const user = useAuthStore((state) => state.user);
|
||||||
|
|
@ -40,8 +41,9 @@ export default function EditProfileScreen() {
|
||||||
const trimmedPhone = updatedPhone.trim();
|
const trimmedPhone = updatedPhone.trim();
|
||||||
const trimmedCountry = updatedCountry.trim();
|
const trimmedCountry = updatedCountry.trim();
|
||||||
|
|
||||||
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
|
if (trimmedPhone) {
|
||||||
const errMsg = "Phone number must be between 7 and 15 digits";
|
const errMsg = validatePhoneNumber(trimmedPhone, undefined, trimmedCountry);
|
||||||
|
if (errMsg) {
|
||||||
if (Platform.OS === "web") {
|
if (Platform.OS === "web") {
|
||||||
window.alert(errMsg);
|
window.alert(errMsg);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -49,6 +51,7 @@ export default function EditProfileScreen() {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue