Javascript - 删除粘贴上的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11943924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - Removing spaces on paste
提问by Matt
I have an input text field with a maxlength of 10. The field is for Australian phone numbers (10 digits). Phone numbers are generally split up into the following syntax
我有一个最大长度为 10 的输入文本字段。该字段用于澳大利亚电话号码(10 位数字)。电话号码通常分为以下语法
12 12345678
If someone copies the above and pastes that into my input field, it obviously leaves the last digit out and keeps the space.
如果有人复制上面的内容并将其粘贴到我的输入字段中,它显然会保留最后一位数字并保留空格。
Is there a way to remove any spaces right before it is pasted into the input box? Or is there another work around?
有没有办法在将空格粘贴到输入框中之前删除任何空格?或者还有其他解决方法吗?
Thanks in advance.
提前致谢。
采纳答案by Paul
This should work for you:
这应该适合你:
HTML
HTML
<input type="text" id="phone" maxlength="10" />?
JavaScript
JavaScript
var phone = document.getElementById('phone'),
cleanPhoneNumber;
cleanPhoneNumber= function(e)
{
e.preventDefault();
var pastedText = '';
if (e.clipboardData && e.clipboardData.getData)
{// Standards Compliant FIRST!
pastedText = e.clipboardData.getData('text/plain');
}
else if (window.clipboardData && window.clipboardData.getData)
{// IE
pastedText = window.clipboardData.getData('Text');
}
this.value = pastedText.replace(/\D/g, '');
};
phone.onpaste = cleanPhoneNumber;
Fiddle: http://jsfiddle.net/y6TYp/6/
小提琴:http: //jsfiddle.net/y6TYp/6/
Updatennnnnn had a great point regarding Australian phone numbers, updating replace regex.
更新nnnnnn 对澳大利亚电话号码有一个很好的观点,更新替换正则表达式。
回答by DarK
Approaching the task you described you want to make sure that your code works in different browsers with or without javascript so I would do the following:
处理您描述的任务时,您希望确保您的代码在使用或不使用 javascript 的不同浏览器中工作,因此我将执行以下操作:
Set the maxlength to 11 - maxlength is an HTML attribute and you need to make sure that with or without javascript the data entered by the user is not lost. If the user enters 11 digits and there is no javascript - you will need to capture the data and cleanse it from the server side. Well, server side validation is mandatory.
If the javascript is there I would use the jquery function to set the maxlength and ensure that the spaces are removed, so say you have a field with ID = 'title': on document ready you write the following code:
$('input#title').attr('maxLength','10').keypress(limitMe);
function limitMe(e) { if (e.keyCode == 32) { return true; } return this.value.length < $(this).attr("maxLength"); }
将 maxlength 设置为 11 - maxlength 是一个 HTML 属性,您需要确保无论是否使用 javascript,用户输入的数据都不会丢失。如果用户输入 11 位数字并且没有 javascript - 您将需要捕获数据并从服务器端清理它。好吧,服务器端验证是强制性的。
如果 javascript 在那里,我将使用 jquery 函数来设置 maxlength 并确保删除空格,因此假设您有一个 ID = 'title' 的字段:在文档准备好时,您编写以下代码:
$('input#title').attr('maxLength','10').keypress(limitMe);
function limitMe(e) { if (e.keyCode == 32) { return true; } return this.value.length < $(this).attr("maxLength"); }
Obviously if you have numerous fields you just decorate all of the input fields with the same class attribute and then apply it to all the fields like so:
显然,如果您有多个字段,您只需使用相同的类属性装饰所有输入字段,然后将其应用于所有字段,如下所示:
$('input.limited').attr('maxLength','10').keypress(limitMe);
回答by karancan
This function will help you do it:
此功能将帮助您做到:
function removeSpaces(string) {
return string.split(' ').join('');
}
Alternatively, using jQuery, you can use the method described here: Delete white spaces in textbox when copy pasted with jquery
或者,使用 jQuery,您可以使用此处描述的方法: 使用 jquery 复制粘贴时删除文本框中的空格