Javascript Javascript从文本框值中删除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3960701/
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 to remove spaces from a textbox value
提问by TMS
I've searched around for this a lot and can't find a specific example of what I'm trying to do. Basically I want to get the value of a textbox, by the name of the text box (not id). Then I want to remove ALL spaces, not just leading or trailing, but spaces in the text too. For example for this html code:
我已经搜索了很多,但找不到我正在尝试做的具体示例。基本上我想通过文本框的名称(不是 id)获取文本框的值。然后我想删除所有空格,不仅仅是前导或尾随,还有文本中的空格。例如对于这个 html 代码:
<INPUT style="TEXT-ALIGN: right;" onkeyup="fieldEdit();" onchange="fieldChange();" NAME="10010input" VALUE="4 376,73" readonly >
I have this javascript:
我有这个 javascript:
var val = document.CashSheet.elements["10010input"].value;
val2 = val.replace(<someregex>, '');
alert(val2);
But I've tried many available regex expressions that remove all spaces, but all only seem to remove leading and trailing spaces, for example the value above 4 376,73 should read as 4376,73 in the alert above but it doesn't. After looking into this normal regex for removing spacesfrom a text field contents is not working and I believe its because its being populated in Polish localised settings on the web server. What \u char/regex exp do I need to capture for a "Polish space" so to speak, in ascii it comes up as 20 but the regex exps that most people are suggesting for spaces does not capture it.
但是我已经尝试了许多可用的正则表达式来删除所有空格,但似乎都只删除了前导和尾随空格,例如,在上面的警报中,4 376,73 以上的值应该读作 4376,73 但它没有。在研究了这个用于从文本字段内容中删除空格的正常正则表达式后,它不起作用,我相信它是因为它被填充在网络服务器上的波兰语本地化设置中。我需要为“波兰语空间”捕获什么 \u char/regex exp 可以这么说,在 ascii 中它显示为 20,但大多数人建议用于空格的正则表达式 exp 没有捕获它。
回答by Tim Down
You can use document.getElementsByName
to get hold of the element without needing to go through the form, so long as no other element in the page has the same name. To replace all the spaces, just use a regular expression with the global flag set in the element value's replace()
method:
您可以使用document.getElementsByName
来获取元素而无需通过表单,只要页面中没有其他元素具有相同的名称。要替换所有空格,只需使用在元素值的replace()
方法中设置全局标志的正则表达式:
var el = document.getElementsByName("10010input")[0];
var val = el.value.replace(/\s/g, "");
alert(val);
回答by Abel Tamayo
You need to "generalize" that regexp you're using so it's applied to all matches instead of just the first. Like this:
您需要“概括”您正在使用的正则表达式,以便将其应用于所有匹配项,而不仅仅是第一个匹配项。像这样:
val = val.replace(/\s/g, '')
Notice the 'g' that modifies the regexp so it becomes "general".
请注意修改正则表达式的“g”,使其变为“通用”。
回答by James Lawruk
Here is a function I use to replace spaces.
这是我用来替换空格的函数。
function removeSpaces(val) {
return val.split(' ').join('');
}
回答by yglodt
Another, more jQuery'ish option:
另一个更像 jQuery 的选项:
$(".stripspaces").keyup(function() {
$(this).val($(this).val().replace(/\s/g, ""));
});
回答by Muddasir Abbas
Try This
尝试这个
<script language="JavaScript">
function RWS(str){
return str.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
}
</script>
<form name=f1ex>
<textarea name="t2" rows="5" onChange="this.value=RWS(this.value)"></textarea>
</form>
回答by R?sh?Kêsh Kümar
Try This:-You Can try to Write or copy paste any string including white space, it's Removed Automatically.
试试这个:-您可以尝试写入或复制粘贴任何字符串,包括空格,它会自动删除。
/* Not Allow Spcace Type in textbox */
function AvoidSpace(event) {
var k = event ? event.which : window.event.keyCode;
if (k == 32) return false;
}
/* Remove Blank Space Automatically Before, After & middle of String */
function removeSpaces(string) {
return string.split(' ').join('');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input placeholder="Enter Your Text" type="text" onkeypress="return AvoidSpace(event);" onblur="this.value=removeSpaces(this.value);">
</form>
回答by Amr Badawy
you can use this jQuery method http://api.jquery.com/jQuery.trim/val = $.trim(val);
OR val = jQuery.trim(val);
您可以使用此 jQuery 方法http://api.jquery.com/jQuery.trim/val = $.trim(val);
或val = jQuery.trim(val);