Check whether an NSString contains a special character and a digit

别说谁变了你拦得住时间么 提交于 2019-11-30 09:23:06
Maulik

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.");
}

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;
}

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");
}

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;

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;}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!