fix(profile): validate and restrict phone number to 10 digits (Issue #41)

This commit is contained in:
ShivaniKhaire 2026-10-01 12:32:13 +05:30
parent 02682595c8
commit 082ad5cb8b
2 changed files with 83 additions and 12 deletions

View File

@ -56,9 +56,20 @@ 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(initialPhone); const [phone, setPhone] = useState(() => formatPhone(initialPhone));
const [country, setCountry] = useState(initialCountry); const [country, setCountry] = useState(initialCountry);
const [nameError, setNameError] = useState("");
const [phoneError, setPhoneError] = useState("");
const [showCountryModal, setShowCountryModal] = useState(false); const [showCountryModal, setShowCountryModal] = useState(false);
useEffect(() => { useEffect(() => {
@ -66,23 +77,63 @@ export default function EditProfileForm({
}, [initialName]); }, [initialName]);
useEffect(() => { useEffect(() => {
setPhone(initialPhone); setPhone(formatPhone(initialPhone));
}, [initialPhone]); }, [initialPhone]);
useEffect(() => { useEffect(() => {
setCountry(initialCountry); setCountry(initialCountry);
}, [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 ( return (
<View style={styles.container}> <View style={styles.container}>
<View style={styles.field}> <View style={styles.field}>
<FieldLabel label="Full Name" required /> <FieldLabel label="Full Name" required />
<BaseTextInput <BaseTextInput
value={fullName} value={fullName}
onChangeText={setFullName} onChangeText={handleNameChange}
placeholder="Your full name" placeholder="Your full name"
autoCapitalize="words" autoCapitalize="words"
autoCorrect={false} autoCorrect={false}
error={nameError}
/> />
</View> </View>
@ -105,12 +156,20 @@ export default function EditProfileForm({
<FieldLabel label="Phone Number" /> <FieldLabel label="Phone Number" />
<BaseTextInput <BaseTextInput
value={phone} value={phone}
onChangeText={setPhone} onChangeText={handlePhoneChange}
placeholder="e.g. +91 9876543210" placeholder="10-digit mobile number"
keyboardType="phone-pad" keyboardType="number-pad"
maxLength={10}
textContentType="telephoneNumber"
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
error={phoneError}
/> />
{!phoneError && (
<Text style={styles.helperText}>
Enter 10-digit mobile number for login and notifications.
</Text>
)}
</View> </View>
<View style={styles.field}> <View style={styles.field}>
@ -126,9 +185,7 @@ export default function EditProfileForm({
<PrimaryButton <PrimaryButton
title="Save Changes" title="Save Changes"
loading={isLoading} loading={isLoading}
onPress={() => { onPress={handleSave}
onSave?.(fullName, phone, country);
}}
/> />
</View> </View>

View File

@ -36,12 +36,26 @@ export default function EditProfileScreen() {
return; 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); setSaving(true);
try { try {
await apiClient.put("/auth/profile", { await apiClient.put("/auth/profile", {
full_name: updatedName.trim(), full_name: trimmedName,
phone_number: updatedPhone.trim() || null, phone_number: trimmedPhone || null,
country: updatedCountry.trim() || null, country: trimmedCountry || null,
}); });
await refreshIdentity(); await refreshIdentity();