Fix: issue 11, error not showing up for under 18

This commit is contained in:
Ayobami
2025-07-06 13:29:50 +01:00
parent 4cc2205d60
commit dcd2a6640d
+22 -5
View File
@@ -45,11 +45,14 @@ export default function SignUpDetailsForm() {
if (!date) return false; if (!date) return false;
const now = new Date(); const now = new Date();
const dob = new Date(date); const dob = new Date(date);
let age = now.getFullYear() - dob.getFullYear(); let age = now.getFullYear() - dob.getFullYear();
const m = now.getMonth() - dob.getMonth(); const monthDiff = now.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && now.getDate() < dob.getDate())) {
if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < dob.getDate())) {
age--; age--;
} }
return age >= 18; return age >= 18;
} }
@@ -132,6 +135,13 @@ export default function SignUpDetailsForm() {
); );
}, [data.password, data.firstName, data.lastName, data.dob]); }, [data.password, data.firstName, data.lastName, data.dob]);
// --- Trigger DOB validation when date changes ---
React.useEffect(() => {
if (data.dob && !isSameDay(data.dob, initialDate.current)) {
trigger("dob");
}
}, [data.dob, trigger]);
// --- Terms and Privacy Modal handlers --- // --- Terms and Privacy Modal handlers ---
function handleAgreeTerms() { function handleAgreeTerms() {
setAgreedToTerms(true); setAgreedToTerms(true);
@@ -257,16 +267,23 @@ export default function SignUpDetailsForm() {
</p> </p>
</div> </div>
<div className='flex flex-col'>
<DatePickerV2 <DatePickerV2
control={control} control={control}
name='dob' name='dob'
min={new Date("1950-01-01")} min={new Date("1950-01-01")}
max={initialDate.current} max={initialDate.current}
setValue={(v) => setValue("dob", v)} setValue={(v) => {
setValue("dob", v);
trigger("dob");
}}
/> />
<p className='mt-2 block text-xs italic text-red-500'> {errors.dob && (
{errors.dob?.message} <p className='mb-1 block text-xs italic text-red-500'>
{errors.dob.message}
</p> </p>
)}
</div>
<div <div
className={`${ className={`${