Issue #41: fix(profile) validate and restrict phone number to 10 digits #49
|
|
@ -48,6 +48,27 @@ const COMMON_COUNTRIES = [
|
||||||
"Spain",
|
"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({
|
export default function EditProfileForm({
|
||||||
initialName = "User Name",
|
initialName = "User Name",
|
||||||
initialEmail = "user@example.com",
|
initialEmail = "user@example.com",
|
||||||
|
|
@ -56,17 +77,8 @@ export default function EditProfileForm({
|
||||||
onSave,
|
onSave,
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
}: EditProfileFormProps) {
|
}: 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 [fullName, setFullName] = useState(initialName);
|
||||||
const [phone, setPhone] = useState(() => formatPhone(initialPhone));
|
const [phone, setPhone] = useState(initialPhone || "");
|
||||||
const [country, setCountry] = useState(initialCountry);
|
const [country, setCountry] = useState(initialCountry);
|
||||||
const [nameError, setNameError] = useState("");
|
const [nameError, setNameError] = useState("");
|
||||||
const [phoneError, setPhoneError] = useState("");
|
const [phoneError, setPhoneError] = useState("");
|
||||||
|
|
@ -77,7 +89,7 @@ export default function EditProfileForm({
|
||||||
}, [initialName]);
|
}, [initialName]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setPhone(formatPhone(initialPhone));
|
setPhone(initialPhone || "");
|
||||||
}, [initialPhone]);
|
}, [initialPhone]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -92,10 +104,10 @@ export default function EditProfileForm({
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePhoneChange = (text: string) => {
|
const handlePhoneChange = (text: string) => {
|
||||||
const cleaned = formatPhone(text);
|
const cleaned = cleanPhoneInput(text);
|
||||||
setPhone(cleaned);
|
setPhone(cleaned);
|
||||||
if (phoneError) {
|
if (phoneError) {
|
||||||
if (!cleaned || cleaned.length === 10) {
|
if (!cleaned.trim() || isValidPhoneNumber(cleaned)) {
|
||||||
setPhoneError("");
|
setPhoneError("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -113,8 +125,8 @@ export default function EditProfileForm({
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimmedPhone = phone.trim();
|
const trimmedPhone = phone.trim();
|
||||||
if (trimmedPhone && trimmedPhone.length < 10) {
|
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
|
||||||
setPhoneError("Phone number must be 10 digits");
|
setPhoneError("Please enter a valid phone number");
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,9 +169,9 @@ export default function EditProfileForm({
|
||||||
<BaseTextInput
|
<BaseTextInput
|
||||||
value={phone}
|
value={phone}
|
||||||
onChangeText={handlePhoneChange}
|
onChangeText={handlePhoneChange}
|
||||||
placeholder="10-digit mobile number"
|
placeholder="e.g. +91 9876543210"
|
||||||
keyboardType="number-pad"
|
keyboardType="phone-pad"
|
||||||
maxLength={10}
|
maxLength={25}
|
||||||
textContentType="telephoneNumber"
|
textContentType="telephoneNumber"
|
||||||
autoCapitalize="none"
|
autoCapitalize="none"
|
||||||
autoCorrect={false}
|
autoCorrect={false}
|
||||||
|
|
@ -167,7 +179,7 @@ export default function EditProfileForm({
|
||||||
/>
|
/>
|
||||||
{!phoneError && (
|
{!phoneError && (
|
||||||
<Text style={styles.helperText}>
|
<Text style={styles.helperText}>
|
||||||
Enter 10-digit mobile number for login and notifications.
|
Enter mobile number with country code for login and notifications.
|
||||||
</Text>
|
</Text>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ 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 from "@/features/profile/components/EditProfileForm";
|
import EditProfileForm, { isValidPhoneNumber } 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";
|
||||||
|
|
@ -40,8 +40,8 @@ export default function EditProfileScreen() {
|
||||||
const trimmedPhone = updatedPhone.trim();
|
const trimmedPhone = updatedPhone.trim();
|
||||||
const trimmedCountry = updatedCountry.trim();
|
const trimmedCountry = updatedCountry.trim();
|
||||||
|
|
||||||
if (trimmedPhone && trimmedPhone.length < 10) {
|
if (trimmedPhone && !isValidPhoneNumber(trimmedPhone)) {
|
||||||
const errMsg = "Phone number must be 10 digits";
|
const errMsg = "Please enter a valid phone number";
|
||||||
if (Platform.OS === "web") {
|
if (Platform.OS === "web") {
|
||||||
window.alert(errMsg);
|
window.alert(errMsg);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue