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

158
Visualizações
Required fields not working as form submits before validation

I am trying to get the required fields working but the form submits before the validation is done and I am not sure how I can fix it. Is there anyway I can fix this?

Apologies in advance for the messy code.

<html>
   <script>
      function submitToAPI(e) {
                  e.preventDefault();
                    $.ajax({
                    type: "POST",
                    url : URL,
                    dataType: "json",
                    crossDomain: "true",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),                 
   </script> 
   
<body>
      <form id="contact-form" method="POST">
      <div class="formcontainer">
      <div id='key'>
         <label for="requestoremail"><strong>Requestor's Email Address</strong></label>
         <input required class="defaultclass" id='requestoremail' type="text" placeholder="Enter your email address" name="requestoremail"  >
         <label for="purpose"><strong>Purpose of Request</strong></label>
         <input required class="defaultclass" id='purpose'type="text" placeholder="Enter purpose of the key " name="purpose" >
      </div>
      <div id='secret'>
         <label for="secretname"><strong>Secret Name</strong></label>
         <input class="secretclass" id='secretname' type="text" placeholder="Enter name of secret" name="secretname" >
         <label for="secretvalue"><strong>Secret Value </strong></label>
         <input class="secretvalueclass" id='secretvalue' type="text" placeholder="Enter value of secret" name="secretvalue" >
      </div>
      <button form="contact-form" type="button" onClick="submitToAPI(event)" class="btn btn-lg" >Submit</button>
      <script>
         $(document).ready(function(){
                $(".defaultclass").attr('required',true);
                $(".secretclass").attr('required',false);
                $("#secret").hide();
                $("#key").show();
      </script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your button has no knowledge of the form it resides inside. You only get automatic validation when the button's type is submit. Because it is of type button, it is just that, a button.

However, you can programatically check if your form is valid before firing your ajax call

// true if your form passes the validity check
const isValid = document.getElementById('contact-form').checkValidity();
about 4 years ago · Juan Pablo Isaza Relatório
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