Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove any spaces while typing into a textbox on a web page

How could I, on the fly, remove spaces entered into a textbox while the person is typing?

like image 264
mrblah Avatar asked Sep 09 '09 19:09

mrblah


People also ask

How can remove space from TextBox using jquery?

The $. trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string.


1 Answers

$(function() {
  var txt = $("#myTextbox");
  var func = function() {
    txt.val(txt.val().replace(/\s/g, ''));
  }
  txt.keyup(func).blur(func);
});

You have to additionally handle the blur event because user could use context menu to paste ;)

like image 198
Josh Stodola Avatar answered Oct 12 '22 07:10

Josh Stodola