Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an NSString contains a special character and a digit

I need to check whether a string contains one uppercase letter, one lower case letter, one integer and one special character. How do I check?

like image 403
jyothi Avatar asked Jul 18 '11 08:07

jyothi


People also ask

How do I find special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

Without any additional frameworks:

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];

if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) {
    NSLog(@"This string contains illegal characters.");
}

You could also use a regex (this syntax is from RegexKitLite: http://regexkit.sourceforge.net):

if ([aString isMatchedByRegex:@"[^a-zA-Z0-9]"]) {
    NSLog(@"This string contains illegal characters.");
}
like image 83
Maulik Avatar answered Sep 22 '22 01:09

Maulik