admin管理员组文章数量:1417673
here is my html:
<input class="form" type="text" placeholder="test" />
and my JS
var str = $(".form").val();
var newStr = str.replace(/\s+/g, '');
there is no space when user input text. How to I create the javascript? anybody help? Thank you
here is my html:
<input class="form" type="text" placeholder="test" />
and my JS
var str = $(".form").val();
var newStr = str.replace(/\s+/g, '');
there is no space when user input text. How to I create the javascript? anybody help? Thank you
Share Improve this question edited Jul 5, 2017 at 3:15 dedi wibisono asked Jul 5, 2017 at 3:08 dedi wibisonodedi wibisono 5335 gold badges12 silver badges23 bronze badges 7-
1
you can use
.trim()
– guradio Commented Jul 5, 2017 at 3:09 - So you want to prevent spaces or trim out the spaces? – Andrew Li Commented Jul 5, 2017 at 3:10
-
Use the string method
.trim()
. What you're doing is removing ALL the spaces in the string. – Gerardo Commented Jul 5, 2017 at 3:10 - oh thank you. Yes I want remove all spacing – dedi wibisono Commented Jul 5, 2017 at 3:12
-
var newStr = str.replace(/\s+/g, '');
should do what you want – Jaromanda X Commented Jul 5, 2017 at 3:12
1 Answer
Reset to default 4If you want to remove starting and ending spaces use trim()
var value = " some text to trim ";
console.log(value.trim());
If you want to remove all the spaces in entire string use replace
var value = "Some text to replace spaces";
var newStr = value.replace(/\s+/g, '');
console.log(newStr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
本文标签: jqueryRemove all space on input text using javascriptStack Overflow
版权声明:本文标题:jquery - Remove all space on input text using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745266302a2650623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论