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

Remove particular characters from a field.

Status
Not open for further replies.

LittleNFiesty

Technical User
Sep 26, 2006
46
US
I want to remove any colons, and/or semi-colons from the below input field. Is there a simple javascript I can do for this?

<input type="text" name="txtSearch" id="txtSearch" style="width: 100px;" class="BodytextXSm" value="Search" onfocus="if (this.value == 'Search') this.value = '';" />
 
<string>.replace uses regular expressions to match substrings. Consider:
Code:
var a = "abc:def;ghi"
a=a.replace(/:|;/g,"")
alert(a)
The regular expression /:|;/g says to replace all occurrences of ":" or ";" with "".

_________________
Bob Rashkin
 
I'm not that familar with javascript. I want it to strip those characters out of the field. So...how do I add that code to the input field:

<input type="text" name="txtSearch" id="txtSearch" style="width: 100px;" class="BodytextXSm" value="Search" onfocus="if (this.value == 'Search') this.value = '';" />

or do I have to add another string that activates when the above input value is sent?
 
I'm a rank novice, myself. What are you trying to do? Are you trying to remove those characters after the user has entered text that might contain them?

_________________
Bob Rashkin
 
Yes. The field is a search input field, and I want to remove certian characters that could have been put in the field after the user has entered them.
 
If you want to enforce it character-by-character, you can add onkeyup handling
[tt] onkeyup="this.value=this.value.replace(/:|;/g,'');"[/tt]
If you want a more lax control, you can sure just add onblur to do the same.
[tt] onblur="this.value=this.value.replace(/:|;/g,'');"[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top