Click to See Complete Forum and Search --> : Replacing tilde (~) with a space


mh53j_fe
10-21-2003, 01:07 PM
Currently, I am checking for the tilde and launching a message box alerting the user that the text has a carriage return. The code is below. I want to change this code to strip the tilde out of the text via Javascript.

If javascript finds the tilde, I want it to strip it out and replace it with a space (" "). How can I do that?

Here is the code I am currently using:

function isTilde( field )
{
var l_field_value = field.value;
var l_field_str = l_field_value.toString();
var l_oneChar;
for(var i = 0; i < l_field_str.length; i++ )
{
l_oneChar=l_field_str.charAt(i);
if(l_oneChar=="~")
{
return false; //How can I replace the tilde with space?
}
}
return true;
}//end function isTilde()

BestZest
10-21-2003, 01:28 PM
This is how I would do it, although it might not be the best way.


var l_field_value = field.value;
var l_field_str = l_field_value.toString();
text = l_field_str.split("~");
field.value = text.join(" ");


Hope this works for you,

BestZest

Jona
10-21-2003, 01:39 PM
Originally posted by BestZest
This is how I would do it, although it might not be the best way.

That's how I'd do it, too. The charAt() method never worked the way I wanted it to, and looping takes more work than simply splitting the object into an array and returning the array as a string. You can also do many other manipulative functions, such as reversing or adding to the array before returning it as a string, in this way.

[J]ona

Charles
10-21-2003, 03:12 PM
<input type="text" onchange="this.value = this.value.replace(/~/g, ' ')">

Jona
10-21-2003, 03:13 PM
Of course, you can always count on Charles to prove you wrong... :D

[J]ona

nkaisare
10-21-2003, 03:15 PM
Originally posted by Jona
Of course, you can always count on Charles to prove you wrong... :D

[J]ona
He didn't prove you wrong; he provided a more elegant way to do it.

Jona
10-21-2003, 03:16 PM
Originally posted by nkaisare
He didn't prove you wrong; he provided a more elegant way to do it.

Whatever... lol. (I'm not offended in any way, just in case you think I was.) Thanks, Charles. :)

[J]ona

mh53j_fe
10-21-2003, 03:20 PM
Jona, Charles, BestZest -- Thank you to all! Here is what I am using. This works perfectly....

function isTilde( field )
{
var l_field_value = field.value;
var l_field_str = l_field_value.toString();
l_field_str = l_field_str.split("~");
field.value = l_field_str.join(" ");
return true;
}//end function isTilde()