Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
Email is not sent even if the error validation object is empty on React Js

I'm trying to send an email when the object error is empty but is not working, even if it is null and doesn't contain any error.

I've checked the validation with an console log, but is giving up the errors that are missing, but I don't know where is the cach.

Here is the contact form function:

import React, { useState, useRef } from 'react'
import './ContactForm.scss'
import emailjs from "emailjs-com"
import '../../shared/../../components/pages/Form/shared/LoginRegister.scss'
import * as styles from '../Form/Globals.module.scss'
import ContactValidation from '../../../utils/ContactValidation'

export default function ContactForm() {

    const [values, setValues] = useState({ name: "", email: "", phone: "", subject: "", description: "" });
    const [errors, setErrors] = useState({});

    const form = useRef();

    const ContactHandler = (e) => {
        e.preventDefault();
        setErrors(ContactValidation(values));

        if (errors === 0 || errors === {} || errors === null || errors === []) {
            emailjs.sendForm('api_hidden', 'swiftquiz_template', form.current, 'api_hidden')
                .then((result) => {
                    console.log(result.text);
                    alert("Sent Successfully ^_^");
                }, (error) => {
                    console.log(error.text);
                });

            setValues({ name: "", email: "", phone: "", subject: "", description: "" });
        }

        else {
            console.log("All Fields are required");

        }

    }

    return (
        <>
           {i_removed_components_for_simpicity}
        </>
    )
}

Here is the validation:

export default function ContactValidation(values){

    let errors = {};

    if(!values.name.trim()){
        errors.name = "Name is required."
    }
    if(!values.email){
        errors.email = "Email is required."
    }else if(!/\S+@\S+\.\S+/.test(values.email)){
        errors.email = "Email is invalid.";
    }
    
    if(!values.subject.trim()){
        errors.subject = "Subject is required."
    }
    if(!values.description.trim()){
        errors.description = "Description is required."
    }
    return errors;

}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!