Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove spaces

Status
Not open for further replies.

iffoiffy

Programmer
Feb 24, 2005
67
CA
Hi,

Just like removespaces function is there a simple function in javascript that remove both the right and left trailing spaces ..........

thanks
 

This simple little function will do just what you want...

Code:
function removeSpaces(strToTrim)
{
  return(strToTrim.replace(/^\s+|\s+$/g, ''));
}

It uses a regular expression (as the post from GUJUm0deL suggests) and expects to be called like:

Code:
myStringWithNoSpaces = removeSpaces(myStringWithSpaces);

Cheers,
Jeff

 
That should probably include trimming tabs and newlines from the ends of the string, too, shouldn't it?

Lee
 
I tried your function but it gives me error

document.addphone.retail_price.value = removeSpaces(document.addphone.retail_price);

or with out semi colon

document.addphone.retail_price.value = removeSpaces(document.addphone.retail_price)


saying object does not support the property..
 


This will work (assuming you have only the one form in your HTML document:

Code:
document.forms[0].addphone.retail_price.value = removeSpaces(document.forms[0].addphone.retail_price.value);

Cheers,
Jeff
 
Wait, is "addphone" the name of your form? If so, it should be this:
Code:
document.addphone.retail_price.value = removeSpaces(document.addphone.retail_price[red].value[/red]);

Adam

recursion (n.) See recursion.
 
That's the second time you've caught me blatantly telling falsehoods in as many days it seems Adam! *sigh*

Jeff
 
Is it? Sorry about that, Jeff. I meant no disrespect.

Adam

recursion (n.) See recursion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top