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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing characters in text field

Status
Not open for further replies.

bryanfl100

Technical User
Dec 2, 2005
17
US
I have the current code for a text field that automatically replaces an "&" for "and" if someone types it in the field. Is there a way to add additional characters such as "@" for "at":


The current code is:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function replaceChars(entry) {
out = "&"; // replace this
add = "and"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.group.SpecialInstructions.value = temp;
}
// End -->
</script>
<form name="group" method="POST" action="<textarea name="SpecialInstructions" rows="3" cols="49" onBlur="replaceChars(this.value);">Number of computers: </textarea></form>
 
change your function to this:

Code:
function replaceChars(entry, out, add) {
    temp = entry.value; // temporary holder

    while (temp.indexOf(out)>-1) {
        pos= temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add +
        temp.substring((pos + out.length), temp.length));
    }
    entry.value = temp;
}

and call it like this:

Code:
 onBlur="replaceChars(this, '&', 'and');"



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
That works good but how do I add another character to the script to include replacing "@" for "at"?
 
Yeah I was trying to do both in the same call function. Basically having both replacements in the same function for '&' to 'and' and '@' to 'at'. Does that make sense?
 
new function, cleaned yours up a bit...

Code:
function replaceChars( entry ) {
    var newVal = entry.value;
    newVal.replace( "&", "and" );
    newVal.replace( "@", "at" );
    entry.value = newVal;
}

this should do the trick...



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
I must have missed something on your new script ... could you post the whole script?
 
Slight twist and amendment and further generalizing the exellent cLFlaVA's realization.
[tt]
function replaceChars( entry ) {
var newVal = entry.value;
newVal=newVal.replace( [blue]/&/g[/blue], "and" );
newVal=newVal.replace( [blue]/@/g[/blue], "at" );
entry.value = newVal;
}
[/tt]
It is understood the onblur call using "this" as argument, onblur="replaceChars(this)".
 
sorry bud, i was being lazy :(

this works:

Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript"><!--
function replaceChars( entry ) {
    var newVal = entry.value;
    var re = new RegExp( "&", "g" );
    newVal = newVal.replace( re, "and" );
    var re = new RegExp( "@", "g" );
    newVal = newVal.replace( re, "at" );
    entry.value = newVal;
}//-->
</script>
</head>

<body>
<form name="group" method="POST" action="[URL unfurl="true"]https://www.test.com">[/URL]
<textarea name="SpecialInstructions" rows="3" cols="49" onBlur="replaceChars(this);">
Number of computers: </textarea></form>
</body>

</html>



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
tsuji's and mine are identical - his is shorter though because he uses implied regular expressions.

both solutions are fine and will work the same.



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top