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

196
Visualizações
How to redirect based on user input javascript

I cant find a working way to redirect the users on my website based on what they type on a text field. I have tried using this code bellow but no success. Can someone please guide me.

function valForm() {
  var firstVal = document.getElementById("nextId");
  if (firstVal.length == 0) {
    error += "Desk Number is required\n";
  }
  if (firstVal == 430) {
    window.location = "https://awebsite";
  }
  if (error) {
    alert(error);
  }
}
<input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" />
<div class="ticketId">
  <input type="text" placeholder="Ticket ID" class="form-control" id="nextId">
</div>
<button class="btn btn-primary display-4" type="button" onclick="valform()">next</button>

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

0

First of all, you have a typo in your button tag. Please change valform() to valForm(). And there are some mistakes in your function as well.

  • Define variable error before it is used in valForm function.
  • var firstVal = document.getElementById("nextId") should be var firstVal = document.getElementById("nextId").value;
  • window.location= "https://awebsite" should be window.location.href = "https://awebsite";

Below is full code.

function valForm() {
    var firstVal = document.getElementById("nextId").value;
    var error = ''
    if (firstVal.length == 0) {
      error += "Desk Number is required\n";
    }

    if (firstVal == 430) {
      window.location.href = "https://awebsite";
    }

    if (error) {
      alert(error);
    }
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

  1. JS is case sensitive so valForm both places
  2. Inputs have value so var firstVal = document.getElementById("nextId").value;
  3. You did not define error
  4. You can cast to number using the unarticulated plus or other methods

The window.location is however ok to use without the .href but it is recommended to use location.href or window.location.href just to get used to the .href if you ever want to READ the string

I suggest you use a submit event instead and use addEventListener to not have inline event handlers. I wrapped the elements in a form tag to use the form's submit event

document.getElementById("myForm").addEventListener("submit", function(e) {
  e.preventDefault(); // stop submission
  let error = "";
  var firstVal = document.getElementById("nextId").value;
  console.log(firstVal)
  if (firstVal.length == 0) {
    error += "Desk Number is required\n";
  }
  if (+firstVal === 430) {
    window.location = "https://awebsite";
  }
  if (error) {
    alert(error);
  }
})
<form id="myForm">
  <input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" />
  <div class="ticketId">
    <input type="text" placeholder="Ticket ID" class="form-control" id="nextId">
  </div>
  <button class="btn btn-primary display-4">next</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

As of right now, you are only holding a DOM reference of the input.

var firstVal = document.getElementById("nextId");.

You have yet to get the value.

var firstVal = document.getElementById("nextId").value;.

Also note that you are missing a camel-case notation in your function call. JavaScript is case sensitive.

<button class="btn btn-primary display-4" type="button" onclick="valform()">next</button>.

should be:

<button class="btn btn-primary display-4" type="button" onclick="valForm()">next</button>

Full code:

function valForm() {
  var firstVal = document.getElementById("nextId").value;
  if (firstVal.length == 0) {
    error += "Desk Number is required\n";
  }
  if (firstVal == 430) {
    window.location.href = "https://awebsite";
  }
  if (error) {
    alert(error);
  }
}
<input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" />
<div class="ticketId">
  <input type="text" placeholder="Ticket ID" class="form-control" id="nextId">
</div>
<button class="btn btn-primary display-4" type="button" onclick="valForm()">next</button>

I will have to assume that error is defined somewhere outside of the code you provided, otherwise it will throw an error of error being undefined.

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