Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda