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.
Alphanumeric Characters and Alphanumeric Password Requirements. An alphanumeric password contains numbers, letters, and special characters (like an ampersand or hashtag).
Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.
The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
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 \
.
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!');
You can try this for all special characters
/^[0-9a-zA-Z\s\r\n@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?]+$/;
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With