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.
switch(e.target.name){
case 'name':
isNameValid(e.target.value);
break;
case 'email':
isEmailValid(e.target.value);
break;
case 'employeeId':
isEmployeeIdVaild(e.target.value);
break;
case 'joiningDate':
isDateValid(e.target.value);
}
Employee Validation
You are viewing a single comment's thread. Return to all comments →
import React, { useState } from "react";
const initialInputState = { name: '', email: '', employeeId: '', joiningDate: '', }
const initialErrorState = { name: true, email: true, employeeId: true, joiningDate: true, }
function EmployeeValidationForm() { const [inputs, setInputs] = useState(initialInputState); const [errors, setError] = useState(initialErrorState);
const isNameValid = (name) => { const isValid = name.length >= 4 && name.match(/^[A-Za-z ]+$/).length; setError(currentErrors => ({ ...currentErrors, name: !isValid, })) }
const isEmailValid = (email) => { const isValid = email.includes("@") && email.endsWith(".com"); setError((currentErrors) => ({ ...currentErrors, email: !isValid, })) }
const isEmployeeIdVaild = (employeeId) => { const isValid = employeeId.length === 6 && Number.isInteger(+employeeId); setError((currentErrors) => ({ ...currentErrors, employeeId: !isValid })) }
const isDateValid = (date) => { const today = new Date("2025-04-11"); const isValid = new Date(date) <= today; setError((currentErrors) => ({ ...currentErrors, joiningDate: !isValid, })) }
const handleChange = (e) => { setInputs((currentInputs) => ({ ...currentInputs, [e.target.name]: e.target.value }));
}
const handleSubmit = (e) => { e.preventDefault(); setInputs(initialInputState); setError(initialErrorState); }