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

337
Visualizações
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 à 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