Remove spaces from an input field with JavaScript

By | December 10, 2008

This is a small snippet to remove the spaces from an input field using JavaScript. It can be useful when you want a value without spaces, or when you want to replace the spaces from the input field with other characters.

The JavaScript code:

The input field:

The spaces will be stripped from the input field in real-time, as the user types.

12 thoughts on “Remove spaces from an input field with JavaScript

  1. Matt Smith (@mattsmith24)

    I must have searched through a hundred sites on Google for this, and yours is the only one that actually worked. THANK YOU. (P.s. onKeyUp works quite nicely too)

  2. Giovka

    Thank you, works good, but unfortunately not removes the space when it’s at the end of a string (for example: if you not remove the space at the end of an email address, this will not valid for the regex that checks the field.
    I’m a beginner with the regex, someone know how to do that?

  3. Sree

    does not allow to modify the pasted text on to the input text box

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.