We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Take special characters out of textbox

kdeutsch asked
Medium Priority
Last Modified: 2012-05-06
I can keep special characters out with the following method but it has one giant shortfall.
When you click in the textbox to change something it automatically send you to end of textbox after one letter and you cannot go back with the arrow keys.  How can I keep them from typing some special characters and allow them to press on back arrow or click in the middle of a typed textbox to edit existing information.
txtdescribe.Attributes.Add("onkeyup", "this.value=this.value.replace(/[^\w - \ . \ ? \ ,]+/,'')")

Open in new window

Watch Question

txtdescribe.Attributes.Add("onblur", "this.value=this.value.replace(/[^\w - \ . \ ? \ ,]+/,'')")
this will replace the characters when then leave the textbox


This somewhat works.  Onw problem I am having is that when I put in a bunch a special characters spread through a sentence it does not get rid of them all, but if I click back in and then click out agian it keeps getting rid of them over multiple click in and outs.  Do I have something wrong with my syntax.
The only spcial charcters I want to allow them are the following.   . , ? - ! " &


Could you also expalin the syntax of this such as what (/{^\w) means.  Thanks
i think you need a g for global in there ill look up the syntax
try this
txtdescribe.Attributes.Add("onblur", "this.value=this.value.replace(/[^\w - \ . \ ? \ ,]+/g,'')")
the \w means
Matches any word character
here is a ref

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Got it, thanks for the information, the solution works great, better than what I had before and Now I know more about building them instead of just copying them.


Great solution better than I had
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.