Delete white spaces in textbox when copy pasted with jquery

女生的网名这么多〃 提交于 2020-01-23 08:02:53

问题


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


回答1:


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,""));




回答2:


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

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



来源:https://stackoverflow.com/questions/8317549/delete-white-spaces-in-textbox-when-copy-pasted-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!