Remove spaces from an input field with JavaScript

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.

Published by Cristian Radulescu

PHP // MySql // Linux

Join the Conversation


  1. 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?

Leave a comment

Leave a Reply

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