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.

- 4,948
- 31
- 95
- 161
-
my bad, didn't read the question correctly. Do you want different filters for alpha-numeric and non-alpha-numeric characters? – Fabrício Matté May 04 '12 at 21:31
5 Answers
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 \
.

- 4,707
- 7
- 35
- 63

- 1,454
- 1
- 10
- 11
-
this could be the list of special characters. !@#$%^&*()+=-[]\';,./{}|":<>? – Abbas May 04 '12 at 21:31
-
can you merge the alpha-numeric and special characters in one regex, i tried this (/^[a-zA-Z_0-9@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]+$/) but not working... – Abbas May 04 '12 at 22:06
-
^[a-zA-Z_0-9@\!#\$\^%&\*\(\)+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+$ ... there is a white space after the "?" ... that is important – Stefan Dochow May 04 '12 at 22:33
This will do what you want.
function validate()
{
var val = <my string>;
if (val == '')
alert('String is empty!');
else if (!val.match(/[_\W]/))
alert('String contains only A-Z a-z 0-9 characters!');
else if (!val.match(/^\w[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]/))
alert('String contains your predefined characters only!');
}
Note that both regexes work on double-negation, returning false in the first match of an illegal character for best performance. First is the negation of the \W
charset which is the negation of \w
. Second is a negation !
of the negation ^
of the pre-defined characters (\w
+ pre-defined chars). Reply if you want any explanation or modifications.
EDIT Here's a regex to match if the string has at least one special character and alpha-numeric characters.
if (val.match(/[^_\W]/) && val.match(/[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]/))
alert('String contains both alpha-numeric and your pre-defined special characters!');
Is it ok or you need it in a single regex pattern?
EDIT This will do it in a single regex:
if (val.match(/(?=.*[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\? ]+?).*[^_\W]+?.*/)
alert('String contains both alpha-numeric and your pre-defined special characters!');

- 69,329
- 26
- 129
- 166
-
thanks its an awesome, but can i get in single regex, what happens is if i check first for the alphanumeric and then special character, is if the field contains special character then it does not pass the alphanumeric test and throws to the else condition, i want a field which SHOULD have alpha-numeric and also special characters. atleast one special character, else it should throw message – Abbas May 04 '12 at 22:36
-
OK, let me see, so you want a regex to test if your string has at least 1 alpha-numeric character and at least 1 non-alpha-numeric character? – Fabrício Matté May 04 '12 at 23:15
-
isn't working with alpha-numeric and special chars constraint, I tried - aabb# which shudn't be allowed – Dev1ce Jun 06 '16 at 10:40
You can try this for all special characters
/^[0-9a-zA-Z\s\r\n@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]+$/;

- 7,630
- 21
- 105
- 159
For the alphanumeric and all special characters Validation
you can use in one regex
This will return true if you have Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
NSString *alphaNumberandSpecialRegex =@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Alphabet and 1 Number:
NSString *alphaNumberandSpecialRegex =@""^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];
Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
NSString *alphaNumberandSpecialRegex =@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberandSpecialRegex];
return [alphaNumberTest evaluateWithObject:@"yourString"];

- 407
- 7
- 16
If want to allow specific special characters with alpha numeric then following regexp will work.You can customize, allowed special characters range as per your requirement.In case of escape characters you need to put in between \ \. In below example \-\ allows '-'.
/^[a-zA-Z0-9?=.*!@#$%^&*_\-\s]+$/
Hope this will help you :).

- 391
- 1
- 3
- 14