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

250
Visualizações
How to maintain a red border around a text input after form submission?

I have the following HTML:

  <form id="contactInfo" name="contactInfo">
        <label>Name: </label>
        <input type="text" name="name"></input><br/><br/>
        <label>E-mail: </label>
        <input type="text" name="email"></input><br/><br/>
        <label>Message:</label><br/>
        <textarea rows="10" cols="80" name="message"></textarea><br/><br/>
        <input type="submit" name="sendMessage" onclick="messageChecker()"></input>
      </form>

And the following Javascript:

 function messageChecker(){
        let x = document.forms["contactInfo"]["name"].value;
        let y = document.forms["contactInfo"]["email"].value;
        let z = document.forms["contactInfo"]["message"].value;
        if (x=="" || y=="" || z==""){
          alert("Please fill out the indicated field(s).");
          if (x==""){
           document.forms["contactInfo"]["name"].style.border = "1px solid red";
          }
          if (y==""){
           document.forms["contactInfo"]["email"].style.border = "1px solid red";
          }
          if (z==""){
           document.forms["contactInfo"]["message"].style.border = "1px solid red";
          }
      
        }
      
      }

So I want the red border to appear when the form is submitted and a field isn't filled in; it appears for less than a second, then disappears, along with the entered text. How can I get it to stay?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Because you are performing a form submit, if you want an app style behavior where you are not submitting forms and then reloading the web page then you need to use AJAX types of data submission.

How to use ajax link instead of submit button for form?

about 4 years ago · Juan Pablo Isaza Relatório

0

maybe use an e.preventDefault() handler to unblock your code?

about 4 years ago · Juan Pablo Isaza Relatório

0

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link. Syntax:

event.preventDefault()

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