Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove spaces from a text input box

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

like image 517
H. Ferrence Avatar asked Mar 20 '13 20:03

H. Ferrence


2 Answers

You can use the syntax:

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

Inside the event handler.

like image 165
Tom Walters Avatar answered Sep 20 '22 14:09

Tom Walters


Replace content of your box in event handler

this.value = this.value.replace(/\s+/g, '');
like image 21
Anoop Avatar answered Sep 23 '22 14:09

Anoop