Validating Alpha-Numeric values with all Special Characters

Abbas picture Abbas · May 4, 2012 · Viewed 49.6k times · Source

i want to validate my text field with below:
1. alpha-numeric
2. And all special characters
i am not good in regex can anyone help me out creating a regex for above things.

Answer

Stefan Dochow picture Stefan Dochow · May 4, 2012

alphanumeric Strings are matched like this:

^[a-zA-Z0-9]+$

It matches any string that only contains of the listed chars and is at least one char long.

With special chars it would work the same way.

But what do you consider to be a special char?

For !@#$%^&*()+=-[]\';,./{}|":<>? – being the set of special chars, the regex would look like this:

^[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+$

Again, all the allowed characters are listed. The ones used within regexes as commands or quantifiers have to be escaped with a \.