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.
There are some error in the test cases for date. Until somebody from HackerRank fix it use the below code for your purpose.
All test cases passed ✅.
importReact,{useEffect,useState}from"react";functionEmployeeValidationForm(){const[user,setUser]=useState({name:"",email:"",employeeId:"",joiningDate:""});const[error,setError]=useState({name:"",email:"",employeeId:"",joiningDate:""});const[isFormValid,setIsFormValid]=useState(true);useEffect(()=>{if(validation()){setIsFormValid(false);}},[user]);constvalidation=()=>{letisValid=true;letnewError={name:"",email:"",employeeId:"",joiningDate:""};if(!user.name.trim()||user.name.trim().length<4){newError.name="Name must be at least 4 characters long and only contain letters and spaces";isValid=false;}constemailRegex=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;if(!user.email.trim()||!emailRegex.test(user.email)){newError.email="Email must be a valid email address";isValid=false;}constempIdRegex=/^\d{6}$/;if(!empIdRegex.test(user.employeeId)){newError.employeeId="Employee ID must be exactly 6 digits"isValid=false;}if(!user.joiningDate.trim()||user.joiningDate>"2024-12-31"){newError.joiningDate="Joining Date cannot be in the future"isValid=false;}setError(newError);returnisValid;}consthandleChange=(e)=>{e.preventDefault();const{name,value}=e.target;setUser((prev)=>({...prev,[name]:value}));};consthandleSubmit=()=>{if(validation()){setUser({name:"",email:"",employeeId:"",joiningDate:""});setError({name:"",email:"",employeeId:"",joiningDate:""});setIsFormValid(false);}else{console.log("Validation Failed.");}};return(<divclassName="layout-column align-items-center mt-20 "><divclassName="layout-column align-items-start mb-10 w-50"data-testid="input-name"><inputclassName="w-100"type="text"name="name"value={user.name}placeholder="Name"data-testid="input-name-test"onChange={handleChange}/>{error.name&&<pclassName="error mt-2">{error.name}</p>}</div><divclassName="layout-column align-items-start mb-10 w-50"data-testid="input-email"><inputclassName="w-100"type="text"name="email"value={user.email}placeholder="Email"onChange={handleChange}/>{error.email&&<pclassName="error mt-2">{error.email}</p>}</div><divclassName="layout-column align-items-start mb-10 w-50"data-testid="input-employee-id"><inputclassName="w-100"type="text"name="employeeId"value={user.employeeId}placeholder="Employee ID"onChange={handleChange}/>{error.employeeId&&<pclassName="error mt-2">{error.employeeId}</p>}</div><divclassName="layout-column align-items-start mb-10 w-50"data-testid="input-joining-date"><inputclassName="w-100"type="date"name="joiningDate"value={user.joiningDate}placeholder="Joining Date"onChange={handleChange}/>{error.joiningDate&&<pclassName="error mt-2">{error.joiningDate}</p>}</div><buttondata-testid="submit-btn"type="submit"onClick={handleSubmit}disabled={isFormValid}>Submit</button></div>);}exportdefaultEmployeeValidationForm;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Employee Validation
You are viewing a single comment's thread. Return to all comments →
There are some error in the test cases for date. Until somebody from HackerRank fix it use the below code for your purpose.
All test cases passed ✅.