automatically remove spaces from text field

If someone is typing in a text field how can I automatically remove any spaces as the form gets submitted?
petewinterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
you can use this function to replace the spaces in text content.

http://www.actionscript.org/forums/showthread.php3?p=33558
function stripSpaces (input) {
	var newString = "";
	for (j=0; j<input.length; j++) {
	   if (input.charAt(j)!=" ") {
		newString += input.charAt(j);
		}
	}
return newString;
}

Open in new window

0
remorinaCommented:
Try the code below which is a very simple script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title>test</title>
<script language="javascript" type="text/javascript">
function removeSpaces(string) {
 return string.split(' ').join('');
}
</script>
</head>
<body>
    <form>
<input type="text" onblur="this.value=removeSpaces(this.value);">
<input type="button" value="Click">
</form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
i-CONICACommented:
Or a PHP version.

$copy = preg_replace('/\s\s+/', ' ', $_POST['copy']);

This will turn this:
My    big  red          apple
into this:
My big red apple
$copy = preg_replace('/\s\s+/', ' ', $_POST['copy']);

//This will turn this:
//My    big  red          apple 
//into this:
//My big red apple

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

i-CONICACommented:
Or if you want to remove ALL spaces, then
$copy = preg_replace('/\s\s+/', '', $_POST['copy']);

This will turn this:
My    big  red          apple 
into this:
Mybigredapple

Open in new window

0
remorinaCommented:
The below will also remove only extra white spaces and keep a single space between words

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title>test</title>
<script language="javascript" type="text/javascript">
function removeSpaces(string) {
 result = string.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
 return result;
}
</script>
</head>
<body>
    <form>
<input type="text" onblur="this.value=removeSpaces(this.value);">
<input type="button" value="Click">
</form>
</body>
</html>

Open in new window

0
petewinterAuthor Commented:
Perfect. Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.