Click here to Skip to main content
15,885,216 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
Posted
Updated 17-May-13 19:40pm
v2
Comments
Sergey Alexandrovich Kryukov 18-May-13 1:43am    
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.
—SA
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: http://www.ultrapico.com/Expresso.htm[^].

See also:
http://en.wikipedia.org/wiki/Regular_expression[^],
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

—SA
 
Share this answer
 
Comments
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?
Sergey Alexandrovich Kryukov 18-May-13 5:48am    
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...
—SA
Mohammed Hameed 18-May-13 5:55am    
You're welcome. Sergey, cross scripted is nothing but cross scripting I'm referring to: https://en.wikipedia.org/wiki/Cross-site_scripting
Sergey Alexandrovich Kryukov 18-May-13 21:47pm    
Sure. Cross-site. :-)
—SA
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: http://stackoverflow.com/questions/756567/regular-expression-for-excluding-special-characters[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-13 1:53am    
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...
—SA
Mohammed Hameed 18-May-13 1:58am    
Thanks Sergey. You're always motivating me to do something more for the Dev community.
Sergey Alexandrovich Kryukov 18-May-13 2:02am    
My pleasure, only if it really helps somehow. It's so great to promote knowledge, thinking and good work and discourage the opposites... :-)
—SA
Sergey Alexandrovich Kryukov 18-May-13 2:00am    
Please see some alternative ideas in my answer.
—SA
Mohammed Hameed 18-May-13 2:04am    
Sure...

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