Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Form object in the console gets deleted on submit

This is my form and I am trying to output the submitted details as an object. The doubt I have is how can I prevent the form from submitting when fields are empty? I want to check whether all the functions above validate() is true or not and then execute the validate function if not true, prevent the form from submitting. The errors that are shown stays there in the form even after submission. I have tried to refresh the window using window.location.reload() but then the object I want is lost by this method. Is there any way I can achieve this?


x = "correct"


function nameValidate(value) {
    namev = value;
    if (namev.length <= 3) {
        
        var nameError = "Enter a valid name"
        document.getElementById("nameerror").innerHTML = nameError
        //return 1;
    } else {
        document.getElementById("nameerror").innerHTML = x
        //return 0;
    }

}

function emailValidate(value) {
    emailv = value;
    if (emailv.length < 5) {
        var emailError = "Enter a valid email"
        document.getElementById("emailerror").innerHTML = emailError
        //return 1;
    } else {
        document.getElementById("emailerror").innerHTML = x
        //return 0;
    }

}

function companyValidate(value) {

    if (value === "") {
        var companyError = "Enter a valid company";
        document.getElementById("companyerror").innerHTML = companyError;
        //return 1;
    } else {
        document.getElementById("companyerror").innerHTML = x;
        //return 0;
    }

}

function phoneValidate(value) {
    phonev = value;
    if ( phonev.length !== 10 ) {
        var phoneError = "Enter a valid 10 digit phone number";
        document.getElementById("phoneerror").innerHTML = phoneError;
        //return 1;
    } else {
        document.getElementById("phoneerror").innerHTML = x;
        //return 0;
    }

}

function validate(e) {

    /* Variables for field values and form */
    var name=document.getElementById("name").value;
    var email=document.getElementById("email").value;
    var company=document.getElementById("company").value;
    var phone=document.getElementById("phone").value;
    var desc=document.getElementById("description").value;
    var project=document.getElementById("project").value;
    var form = document.getElementById("form");
    
    //e.preventDefault();   
    console.log({name,email,company,phone,desc,project});
    form.reset();
 }

 //form.addEventListener("submit",validate(),true);
        

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda