We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- React
- Forms
- Employee Validation
- Discussions
Employee Validation
Employee Validation
Sort by
recency
|
11 Discussions
|
Please Login in order to post a comment
Not able to submit or test. No test cases found. What to do?
I trying so hard for this challenge, its weird because the logic is different, if u see the error carefully u would understand how to abuse it T_T
the logic is,
"joining date" will display two same error at the first, just clone the error. But if "joining date" was correctly inputed, that one error should shows until all field correctly inputed.
THEN, if name, email, employeeId is null that "joining date" error should only shows once, but if u correctly input the "joining date" (while name, email, employeeId is null) all errors of "joining date" should disappear:)
here is my code its work fine but last four case not pass how to fix it
import React, { useEffect, useState } from "react";
function EmployeeValidationForm() {
const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [id, setId] = useState(""); const [date, setDate] = useState(""); const [nameError, setNameError] = useState(true); const [emailError, setEmailError] = useState(true); const [idError, setIdError] = useState(true); const [dateError, setDateError] = useState(true); const [disableSubmit, setDisableSubmit] = useState(true);
useEffect(() => { if (name && name?.length >= 4 && /^[a-zA-Z .]+$/.test(name)) { setNameError(false); } else { setNameError(true); } if (email && email.includes("@gmail.com")) { setEmailError(false); } else { setEmailError(true); } if (id && id.length >= 6) { setIdError(false); } else { setIdError(true); } const inputDate = new Date(date); const currentDate = new Date() if (date && currentDate >=inputDate) { setDateError(false); } else { setDateError(true); }
}, [name, email, id, date])
useEffect(() => { if (!nameError && !emailError && !idError && !dateError) { setDisableSubmit(false) } else { setDisableSubmit(true) } }, [nameError, emailError, idError, dateError])
const handleSubmit =()=>{ setName(""); setDate(""); setEmail(""); setId(""); }
return ( setName(event.target.value)} data-testid="input-name-test" /> {nameError && Name must be at least 4 characters long and only contain letters and spaces
} setEmail(event.target.value)} /> {emailError && Email must be a valid email address } setId(event.target.value)} /> {idError && Employee ID must be exactly 6 digits } setDate(event.target.value)} /> {dateError && Joining Date cannot be in the future } Submit ); }export default EmployeeValidationForm;
This is my solution, I hope it helps