Click here to Skip to main content
15,037,178 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
what can be the best possible regular expression to avoid special characters e.g !'",?/@ etc
Updated 17-May-13 19:40pm
How are they special? What's so special in them. And let me ask you: are the characters like, say, "¶ ※ † ♈ ♞ ♍ ☹" less "special"? They all can be entered in your Web page input, as easily as I just entered then while writing this comment.
Mohammed Hameed 18-May-13 1:45am
Yes, I agree with Sergey. Check my solution below to understand further.

Solution 1 provides right idea. Besides, you may want to do a different thing: instead of validation, you could filter out unwanted character while they are entered. In a Web application, this is done in JavaScript. Just one detail, often forgotten, don't forget to allow "character" #8, backspace.

As to Regex, your problem is one of the basic, so you better need to learn Regex. You can develop and learn Regular Expressions very effectively if you use Expresso:[^].

See also:[^],[^].

Mohammed Hameed 18-May-13 2:08am
+5 for providing link 4 learning Regular expressions. But one thing is that Javascript can be cross scripted and bypassed, am I right?
Thank you, Mohammed.
Yes, JavaScript of course can be bypassed (I don't quite understand "cross-scripted"), so really critical validations are done on server part...
Mohammed Hameed 18-May-13 5:55am
You're welcome. Sergey, cross scripted is nothing but cross scripting I'm referring to:
Sergey Alexandrovich Kryukov 18-May-13 21:47pm
Sure. Cross-site. :-)
Rather than avoid special characters, ideally you have to allow the required characters only. Then, automatically all other characters will be not allowed.

I would recommend you to go through this link for further explanation with examples:[^]
Good idea. I voted 4 this time, just because the link is not the most adequate (Java), but it's easy to find. Regex itself is the same. By the way, this is one of the most elementary regex chores, so asking such questions is not very productive...
Mohammed Hameed 18-May-13 1:58am
Thanks Sergey. You're always motivating me to do something more for the Dev community.
My pleasure, only if it really helps somehow. It's so great to promote knowledge, thinking and good work and discourage the opposites... :-)
Please see some alternative ideas in my answer.
Mohammed Hameed 18-May-13 2:04am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900