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.
// Validate date: must not be in the future and must be a valid date string
const isDateValid = () => {
if (!date) return false;
let dateNow = new Date('2025-04-11').getTime();
const inputDate = new Date(date);
return inputDate <= dateNow;
// Show all errors on initial load
useEffect(() => {
setSubmitted(true);
}, []);
return (
{/* Name input */}
setName(e.target.value)}
/>
{submitted && !isNameValid && (
Name must be at least 4 characters long and only contain letters and spaces.
Employee Validation
You are viewing a single comment's thread. Return to all comments →
This is the clean and clear code for this question with pass all test cases. import React, { useState, useEffect } from "react";
function EmployeeValidationForm() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [empid, setEmpid] = useState(""); const [date, setDate] = useState(""); const [submitted, setSubmitted] = useState(false);
const nameRegex = /^[a-zA-Z\s'-]{4,}/; const empidRegex = /^\d{6}$/;
const isNameValid = nameRegex.test(name.trim()); const isEmailValid = emailRegex.test(email.trim()); const isEmpIdValid = empidRegex.test(empid);
// Validate date: must not be in the future and must be a valid date string const isDateValid = () => { if (!date) return false; let dateNow = new Date('2025-04-11').getTime(); const inputDate = new Date(date); return inputDate <= dateNow;
};
const onSubmit = () => { setSubmitted(true); if (isNameValid && isEmailValid && isEmpIdValid && isDateValid()) { alert("Form submitted successfully!"); } };
// Show all errors on initial load useEffect(() => { setSubmitted(true); }, []);
return ( {/* Name input */} setName(e.target.value)} /> {submitted && !isNameValid && ( Name must be at least 4 characters long and only contain letters and spaces.
)}); }
export default EmployeeValidationForm;