Take special characters out of textbox

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

kdeutschAsked:
Who is Participating?
 
burakiewiczConnect With a Mentor Commented:
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
0
 
burakiewiczCommented:
try
txtdescribe.Attributes.Add("onblur", "this.value=this.value.replace(/[^\w - \ . \ ? \ ,]+/,'')")
 
0
 
burakiewiczCommented:
this will replace the characters when then leave the textbox
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
kdeutschAuthor 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.   . , ? - ! " &
0
 
kdeutschAuthor Commented:
HI
Could you also expalin the syntax of this such as what (/{^\w) means.  Thanks
0
 
burakiewiczCommented:
i think you need a g for global in there ill look up the syntax
0
 
kdeutschAuthor 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.
0
 
kdeutschAuthor Commented:
Great solution better than I had
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.