Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete white spaces in textbox when copy pasted with jquery

I've a textbox of order id(7 digits), which in normal cases you copy paste from email to the textbox, many times you accidently copy one or two white spaces which causing annoying validation error.

I want jquery code in my Layout\MasterPage that does't allow writing white spaces and when copy paste (with keyboard or mouse) numbers with white spaces removes the spaces and keeps the numbers only.

The above should happen only on textboxes with class white-space-is-dead

like image 267
gdoron is supporting Monica Avatar asked Nov 29 '11 20:11

gdoron is supporting Monica


2 Answers

Do

$('.white-space-is-dead').change(function() {   
    $(this).val($(this).val().replace(/\s/g,""));
});

Updated copy of your fiddle.

Note that \s may be more than you like. If so, use a literal white space instead

.replace(/ /g,""));

like image 123
P.Brian.Mackey Avatar answered Oct 05 '22 02:10

P.Brian.Mackey


$('.white-space-is-dead').change(function() {   
    $(this).val($(this).val().replace(/\s/g,""));
});

This will remove all the white spaces using a regular expression.

like image 39
gillyb Avatar answered Oct 05 '22 00:10

gillyb