最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

remove spaces in string using javascript - Stack Overflow

programmeradmin4浏览0评论

I need to do some functions on some text field contents before submitting the form, like checking the validity of the customer registeration code, having the customer name as his code in customers table appending an incrementing number.

I don't want to do it after the form is submitted becuase I need it to be displayed in the code field before submitting the form.

My code:

function getCode(){
    var temp = document.getElementById("temp").value ;
    var d = parseInt(document.getElementById("temp").value) + 1;
    document.getElementById("customernumber").value = d;
    document.getElementById("code").value = document.getElementById("name").value+"-"+ d;
}

It all works fine, but the last line of code developed the code WITH the spaces between the code.

I need to do some functions on some text field contents before submitting the form, like checking the validity of the customer registeration code, having the customer name as his code in customers table appending an incrementing number.

I don't want to do it after the form is submitted becuase I need it to be displayed in the code field before submitting the form.

My code:

function getCode(){
    var temp = document.getElementById("temp").value ;
    var d = parseInt(document.getElementById("temp").value) + 1;
    document.getElementById("customernumber").value = d;
    document.getElementById("code").value = document.getElementById("name").value+"-"+ d;
}

It all works fine, but the last line of code developed the code WITH the spaces between the code.

Share Improve this question edited Jul 1, 2012 at 9:13 Dan 1,88813 silver badges17 bronze badges asked Jul 1, 2012 at 9:09 user1471378user1471378 1
  • Care to post example of text with spaces and how you want it to look like? Also, the "!" character means shouting. Please don't use it so often. – Shadow Wizard Commented Jul 1, 2012 at 9:13
Add a comment  | 

2 Answers 2

Reset to default 18

A couple ways to remove spaces...

Using regex: string.replace(/ /g,'');

Splitting the string by spaces and combining the array with no delimiter:

string.split(' ').join('');

var str = "ab cd ef gh   ";
str = str.replace(/\s+/g,"");
发布评论

评论列表(0)

  1. 暂无评论