javascript 如何在文本框的开头和结尾修剪空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10747119/
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
How to trim a space at the start and end of textbox?
提问by user1394925
I want to trim any spaces at the start of the text box and trim any spaces at the end of the textbox. So I have found this code on a website which is suppose to remove spaces at the start, end and multiple spaces in between:
我想修剪文本框开头的任何空格并修剪文本框末尾的任何空格。所以我在一个网站上找到了这个代码,它假设删除开头、结尾的空格和中间的多个空格:
function trim(s) {
s = s.replace(/(^\s*)|(\s*$)/gi,"");
s = s.replace(/[ ]{2,}/gi," ");
s = s.replace(/\n /,"\n");
return s;
}
My problem is though that first of all which one of the 3 lines of code is the one where it trims spaces in the middle because I don't need that one. But the main question is how do I get the textbox to access this function?
我的问题是,首先 3 行代码中的哪一行是在中间修剪空格的那一行,因为我不需要那一行。但主要问题是如何让文本框访问此功能?
I tried using onkeypress but this hasn't worked, below is what I have tried:
我尝试使用 onkeypress 但这没有用,以下是我尝试过的:
<p>Search: <input type="text" name="questioncontent" onkeypress="return trim(s)" /></p>
So what I want is that for example if this phrase is entered in textbox ' My Name is Pete '. Then it should remove the spaces at the start and end so it reads 'My Name is Pete'. But how do I get this to work?
所以我想要的是,例如,如果在文本框中输入这个短语“我的名字是皮特”。然后它应该删除开头和结尾的空格,使其显示为“我的名字是皮特”。但是我如何让它发挥作用?
UPDATE:
更新:
Found out that trim() is jQuery, so does anyone a javascript equivalent for this which can be hand coded to remove spaces at start and end of textbox?
发现trim() 是jQuery,那么有没有人可以为此编写javascript 等效项,可以手动编码以删除文本框开头和结尾的空格?
回答by HBP
You need to change your HTML :
你需要改变你的 HTML :
<p>Search: <input type="text" name="questioncontent" onchange="return trim(this)" /></p>?
Pass the input element as a parameter to trim and use onchange instead of onkeypress.
将输入元素作为参数传递以修剪并使用 onchange 而不是 onkeypress。
Then trim needs to be :
然后修剪需要是:
function trim (el) {
el.value = el.value.
replace (/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
replace (/[ ]{2,}/gi," "). // replaces multiple spaces with one space
replace (/\n +/,"\n"); // Removes spaces after newlines
return;
}?
This modifies the value of the input element, removing leading and trailing spaces, replacing multiple spaces with a single space, and removing any spaces after newline characters.
这会修改输入元素的值,删除前导和尾随空格,用单个空格替换多个空格,并删除换行符后的所有空格。
JSfiddle : http://jsfiddle.net/jstoolsmith/ZNQQm
JSfiddle:http: //jsfiddle.net/jstoolsmith/ZNQQm
回答by Daedalus
Reading the comments.. it really depends on how the function is used. I attempted to test with it on onkeypress
, however that didn't have your desired effect. I did however get something similarto what you desired, though I'm still unsure if that's what you wanted.
阅读评论.. 这真的取决于函数的使用方式。我试图用它进行测试onkeypress
,但这并没有达到你想要的效果。然而,我确实得到了与您想要的类似的东西,但我仍然不确定这是否是您想要的。
I had to modify the function slightly to work with your code, but basically, on blur, it executes the function, and alters the input text accordingly.
我不得不稍微修改该函数以使用您的代码,但基本上,在模糊时,它会执行该函数,并相应地更改输入文本。
回答by bharati
function trim (el) {
el.value = el.value.
replace (/(^\s*)|(\s*$)/, ""). // removes leading and trailing spaces
replace (/[ ]{2,}/gi," "). // replaces multiple spaces with one space
replace (/\n +/,"\n"); // Removes spaces after newlines
return;
}?