How to remove spaces from a text input box

H. Ferrence picture H. Ferrence · Mar 20, 2013 · Viewed 9.6k times · Source

I have the following jQuery shell which works:

  $('.jq_noSpaces').on('change', function(){
    alert('you changed the value in the box');
  });

My form attributes are id="username" name="username"

How do I use the following jQuery replace function to automatically change remove the spaces from the input field?

str.replace(/\s+/g, '');

Thanks

Answer

Tom Walters picture Tom Walters · Mar 20, 2013

You can use the syntax:

$(this).val($(this).val().replace(/\s+/g, ''));

Inside the event handler.