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?
Found a slightly better implementation. Improvement on Ameesh's answer
- (BOOL)isValidString:(NSString *)string
{
return [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]].location != NSNotFound &&
[string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]].location != NSNotFound &&
[string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;
}
Here is what I would do. Create Regular expressions for every condition you need to check if corresponding value present or not.
i.e. Regular Expression to check if it has one uppercase letter, one lower case letter , one integer and one special character, and so on.
and then use the same string to check against every regular expression if all of them return true you have winner if not then string doesn't match to your criteria.
// Example for the Validating UpperCaseLetter do same for all others with matching 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;}
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.");
}
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;
Maulik's answer is incorrect. That will check for anything OTHER than any alphanumeric character, but doesn't enforce that there be one uppercase letter, one lowercase letter, and one integer. You do in fact have to do 3 checks to verify 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");
}