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

x

Take special characters out of textbox

kdeutsch
kdeutsch asked
on
Medium Priority
503 Views
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

Comment
Watch Question

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

Author

Commented:
Hi,
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.   . , ? - ! " &

Author

Commented:
HI
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,'')")
also
the \w means
Matches any word character
here is a ref
http://regexlib.com/CheatSheet.aspx

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

Ask the Experts

Author

Commented:
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.

Author

Commented:
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.

OR

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.