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

367
Vistas
Using Javascript, how to detect the value typed into a input box while it's being typed

I have this problem to solve

In this form a user types in a value. (Actually, a scanner scans a number and virtually types it - without sending extra keys like Enter)

I need to contantly check - while typing is going on - if the value in the input box is a 8 digit number (starting with "4") and if it is, fire the submit action.

I tried to log any changes. But the code below only logs changes after I leave the input box.

<form action="#" onsubmit="return validateFormOnSubmit(this);">
    <input name="boarding_id" value="" width="600px" onChange="console.log(this.value);">
    <button type="submit" name="action" class="btn btn-primary" value="0">Scan</button>
</form>

Is there a Javascript way to pass the value of the box to a function whenever a single letter is typed?

Note: While the form displays a "scan" button, the goal is to have that button automatically clicked as soon as 8 digits have been entered and been declared valid by a validator function.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It is generally not a good idea to use inline event handlers.

Actually, a scanner scans a number and virtually types it

So, as far as I understand you want to show the result of some scanning function that inputs values and check the input value. Looks like there's not really a need for a change handler. Here's a minimal reproducable example for a dummy scanning function. It uses event delegation for handling the button click.

document.addEventListener(`click`, handle);

function scan(i = 0) {
  const inp = document.querySelector(`[name='boarding_id']`);
  const showIt = document.querySelector(`#showIt`);
  
  if (i < 1) {
    inp.value = 4;  
    i += 1;
  } else {
    const nr = 1 + Math.floor(Math.random() * 9);
    const currentValue = inp.value;
    inp.value += nr;
  }
  
  if (i < 8) {
    showIt.textContent = `Scanning ...`;
    return setTimeout( () => scan(i + 1), 100)
  }
  
  showIt.textContent = `Done!`;
  document.querySelector(`#scan`).removeAttribute(`disabled`);
}

function handle(evt)
{
  if (evt.target.id === `scan`) {
    evt.target.setAttribute(`disabled`, `disabled`);
    return scan();
  }
}
<input name="boarding_id" value="" width="600px" readonly> 
<span id="showIt"></span>
<p><button id="scan">Scan</button></p>

about 4 years ago · Juan Pablo Isaza Denunciar

0


const input = document.querySelector('input');
const demo_variable = document.getElementById('demo_variable');

input.addEventListener('input', updateValue);

function updateValue(e) {
  demo_variable.textContent = e.target.value;
}
<form action="#" onsubmit="return validateFormOnSubmit(this);">
    <input name="boarding_id" value="" width="600px" >
    <button type="submit" name="action" class="btn btn-primary" value="0">Scan</button>
    <p id="demo_variable"></p>
</form>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the input event, which also triggers when editing happens without the keyboard (mouse drag/drop, context menu, other device...).

Use a regular expression to do the verification. You can access the form via the form property of the input element:

<input name="boarding_id" oninput="/^4\d{7}$/.test(this.value) && this.form.submit()">

It is however better practice to bind events not with HTML attributes, but with JS code. For that purpose give the form element an id attribute (like id="frm"), and then:

const form = document.getElementById("frm");
form.boarding_id.addEventListener("input", (e) => /^4\d{7}$/.test(e.target.value) && form.submit());
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