Home : Internet : JavaScript : Remove Spaces

How to Remove Spaces From a Text Field

This page shows you how to remove spaces from a text field. Any spaces entered by the user will be removed when they click anywhere outside the text field. (See also: remove unwanted extra spaces.)

Try it here:

Step 1

Add the following code to your head:

<script language="javascript" type="text/javascript">
function removeSpaces(string) {
 return string.split(' ').join('');

Step 2

Use the following code for your text field:

<input type="text" onblur="this.value=removeSpaces(this.value);">
<input type="button" value="Click">