Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!