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

330
Vistas
JavaScript Remove Whitespace on HTML Input Element

I wrote a small code which removes whitespaces during input.

<input id="my_input">
window.my_input.addEventListener('input', (event) => {
  const cursorPosition = event.target.selectionEnd;
  const modifiedInput = event.target.value.replace(/\s+/g, '');
  
  if(modifiedInput !== event.target.value) {
    event.target.value = modifiedInput;
    event.target.selectionEnd = cursorPosition - 1;
  }
});

https://codepen.io/justpilot/pen/bGYMEqr

It works fine on Desktop. But on mobile (Firefox latest version) I have some issues:

I'm typing "abcabc", double click on the spacebar inserts a dot (.) But because the whitespace was removed, the auto correction removes the last character of my string. Now the Input looks like this: "abcab.". Now I continue to put in abc and I click on the spacebar. The autocorrection kicks in (wants to convert abc to ABC). Now my string looks like this: "abcab.abABC" instead of "abcabc.abc".

Does anyone has a good idea, how to prevent the whitespace on input on mobile devices with auto correction?

My goal is, to prevent the whitespaces on email input. Because it is the only field I have, I don't want to use onblur Event. The Submit button stays deactivated as long as the email field is invalid.

Thank you and best Regards

Dimitri

about 4 years ago · Juan Pablo Isaza
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