Check NSString for special characters
Here the code you can use it to check the string has any special character or not
NSString *string = <your string>;
NSString *specialCharacterString = @"!~`@#$%^&*-+();:={}[],.<>?\\/\"\'";
NSCharacterSet *specialCharacterSet = [NSCharacterSet
characterSetWithCharactersInString:specialCharacterString];
if ([string.lowercaseString rangeOfCharacterFromSet:specialCharacterSet].length) {
NSLog(@"contains special characters");
}
NSCharacterSet * set = [[NSCharacterSet alphanumericCharacterSet] 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");
}