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

210
Visualizações
how can i solve the double submit problem in javascript code

what i would like are 2 input fields with a submit button. the first field is the request for a username, the second field is a password.

if the user presses submit, the value of the fields should be below it. but for a reason it doesn't quite work. if the user presses submit a second, then the result is written after it. and the intention is that the text of the first submit is completely replaced.

also the checker function does not work.

could anyone help?

var titles = [];
var usernameInput = document.getElementById("username");
var passwordBox = document.getElementById("password");
var messageBox = document.getElementById("display");

function CHECKER() {
  if (!user.title.value.match(/[a-zA-Z]$/) && user.title.value != "") {
    user.title.value = "";
    alert("GEEF EEN GELDIGE WAARDE AUB");
  }
}

function insert() {
  titles.push(usernameInput.value);
  titles.push(passwordBox.value);
  clearAndShow();
}

function clearAndShow() {
  usernameInput.value = "";
  passwordBox.value = "";
  messageBox.innerHTML = "";
  messageBox.innerHTML += "De opgegeven gebruiksnaam en wachtwoord zijn: " + titles.join(", ") + "<br/>";
}
<html>

<head></head>

<body>
  <div class="wb-stl-custom3">
    <label for="username">gebruikersnaam:</label>
  </div>
  <div>
    <input id="username" type="text" maxlength="15" onkeyup="CHECKER()">
  </div>
  <div class="wb-stl-custom3" ;>
    <label for="password">wachtwoord:    </label>
  </div>
  <div>
    <input id="password" type="password" maxlength="30" onkeyup="CHECKER()">
  </div>
  <input type="submit" value="CHECH" onclick="insert()" />
  <div class="wb-stl-custom3" , id="display"></div>
</body>

</html>

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

0

I changed your CHECKER() so it will check each input field. Your code was explicitly written to add and not replace content into your titles array. If you want the values to be replaced you will need something like the following:

var titles = [];
var usernameInput = document.getElementById("username");
var passwordBox = document.getElementById("password");
var messageBox = document.getElementById("display");

function CHECKER(inp) {
  if (!inp.value.match(/[a-zA-Z]$/) && inp.value != "") {
    inp.value = "";
    alert("GEEF EEN GELDIGE WAARDE AUB");
  }
}

function insert() {
  titles=[usernameInput.value,
          passwordBox.value];
  clearAndShow();
}

function clearAndShow() {
  usernameInput.value = "";
  passwordBox.value = "";
  messageBox.innerHTML = "";
  messageBox.innerHTML += "De opgegeven gebruiksnaam en wachtwoord zijn: " + titles.join(", ") + "<br/>";
}
<html>

<head></head>

<body>
  <div class="wb-stl-custom3">
    <label for="username">gebruikersnaam:</label>
  </div>
  <div>
    <input id="username" type="text" maxlength="15" onkeyup="CHECKER(this)">
  </div>
  <div class="wb-stl-custom3" ;>
    <label for="password">wachtwoord:    </label>
  </div>
  <div>
    <input id="password" type="password" maxlength="30" onkeyup="CHECKER(this)">
  </div>
  <input type="submit" value="CHECH" onclick="insert()" />
  <div class="wb-stl-custom3" , id="display"></div>
</body>

</html>

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