Check if NSString contains a special character and number

I need to check if a string contains one uppercase letter, one lowercase letter, one integer and one special character. How can i check?

+8
string objective-c cocoa-touch cocoa nsstring
source share
5 answers

Without any additional frameworks:

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

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

 if ([aString isMatchedByRegex:@"[^a-zA-Z0-9]"]) { NSLog(@"This string contains illegal characters."); } 
+15
source share

Found a slightly better implementation. Amish's Better Answer

 - (BOOL)isValidString:(NSString *)string { return [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]].location != NSNotFound && [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]].location != NSNotFound && [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound; } 
+9
source share

Maulik's answer is incorrect. This will check for anything OTHER than any alphanumeric character, but does not guarantee that there is one uppercase letter, one lowercase letter and one whole. In fact, you need to do 3 checks to check each constraint.

 NSCharacterSet *lowerCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"]; NSCharacterSet *upperCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; if ([aString rangeOfCharacterFromSet:lowerCaseChars].location == NSNotFound || [aString rangeOfCharacterFromSet:upperCaseChars].location = NSNotFound || [aString rangeOfCharacterFromSet:numbers].location == NSNotFound) { NSLog(@"This string contains illegal characters"); } 
+4
source share

For Arabic / English

 NSString *regEx = @"^([a-zA-Z0-9\u0600-\u06ff](\\-|\\_|\\.|\\ )?)+$"; NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx]; BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: self.text]; if (!myStringMatchesRegEx) return NO; 
+1
source share

Here is what I will do. Create regular expressions for each condition that you need to check if the corresponding value is present.

i.e. A regular expression to check if it has one uppercase letter, one lowercase letter, one integer and one special character, etc.

and then use the same line to check for each regular expression, if all of them return true, you have a winner, if not, then the line does not meet your criteria.

// Example for Validating UpperCaseLetter does the same for everyone else with the appropriate regular expression.

 -(BOOL) validateOneUpperCaseLetter:(NSString *)string { if ((string == nil) || ([string isEqualToString: @""])) { return NO; } // Change this regEx to one you needed. // this one validates for the "name". NSString *regEx = @"^[a-zA-Z]+(([\\'\\,\\.\\ -][a-zA-Z])?[a-zA-Z]\\s*)*$"; NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx]; BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject: string]; if (!myStringMatchesRegEx) { return NO; } return YES;} 
-one
source share

All Articles