0

I am trying to validate the form and for this i created a component Validation info which is taking form data as input and returns the object of validation error and in form component i created the useState hook with an empty object and while submiting i am trying to update state but it's not working

import validate from "./ValidationInfo"
const Registrationform = ()=>
{
 const[errors, setErrors] = useState({})
 const handleSubmit = (e)=>{
 e.preventDefault();
 setErrors(validate(formData))
 console.log('Error: ', errors)
 }
}

const ValidateInfo = (value) =>
{
    let errors = {}
    if(!value.name.trim())
    {
        errors.name = "Username is required"
    }
    if(!value.email.trim())
    {
        errors.email = 'Email required'
    }else if(!/^[A-Z0-9,_%+-]+@[A-Z0-9,-]+\.[A-Z]{2,}$/i.test(value.email)){
        errors.email = "Email is invalid"
    }
    if(!value.password.trim())
    {
        errors.password = 'Email required'
    }else if(value.password.length < 6){
        errors.password = "Password should contain 6 char"
    }
    return errors;
} 

export default ValidateInfo

0 Answers0