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

73
Visualizações
Ajax vs Bootstrap 5 validation

I'm trying to get value with ajax in bootstrap 5.

Ajax sends "chip" value to php and returns 1 if there is such data in database, 2 if isn't, 0 if input"chip" field was empty (just for testing).

If it's 1 I want it to display information. If 2 I want to submit the form.

It works great but... it stops default bootstrap validation when required "chip" field is empty.

If I uncomment "if (!chip) event.preventDefault()" it's validated but when value 1 is returned it submits form anyway..

Is there way to display default bootstrap information or I have to show my own when data is 0?

Help :)

document.getElementById("chipButton").onclick = function() {
    
    const dataToSend = {
    chip : document.getElementById("chip").value
    }
    fetch(url, {
    method: 'post', 
    mode: 'cors',
    headers: new Headers({
    'Content-Type': 'application/json'
    }),
    body: JSON.stringify(dataToSend)
    })

    .then((response) => {
      return response.json();
    })
    .then((data) => {
      console.log(data)
      if (data.check==1) { 
        pokazKomunikat(data.check)
        } 
      else if (data.check > 1 ) 
        {
          subForm()
        }
      
    });
    
    //if (!chip) event.preventDefault()
    event.preventDefault()

}
function pokazKomunikat(id){
  
  document.querySelector(".chipAlert").innerHTML = 'answer <a href="<?php echo $base_url;?>/aaa/'+id+'/0/0/0"> here</a>';
  document.querySelector(".chipAlert").style.display = "block";
}

function subForm(){
    document.getElementById("chipForm").submit();
}
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