Remove spaces from an input field with JavaScript

December 10, 2008 – 23:31

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:

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

The input field:

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

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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • BlinkList
  • Fark
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • LinkedIn
  • Mixx
  • MySpace
  • Google Bookmarks
  • Live
  • Yahoo! Buzz
  • DZone
  • Twitter
  • Yahoo! Bookmarks

Tags: , ,

  1. One Response to “Remove spaces from an input field with JavaScript”

  2. very good Remove Spaces, Im From Brazil!

    By de musicas on Dec 13, 2008

Post a Comment