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:

function stripspaces(input)
{
  input.value = input.value.replace(/\s/gi,"");
  return true;
}

The input field:

<input onkeydown="javascript:stripspaces(this)" name="field_with_no_spaces" type="text" />

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

About Cristian Radulescu

PHP // MySql // Linux // Android
This entry was posted in JavaScript, Programming and tagged , , . Bookmark the permalink.

One Response to Remove spaces from an input field with JavaScript

  1. de musicas says:

    very good Remove Spaces, Im From Brazil!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>