Check whether an NSString contains a special character and a digit

前端 未结 5 1579
别那么骄傲
别那么骄傲 2020-12-31 18:56

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?

相关标签:
5条回答
  • 2020-12-31 19:07

    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;
    }
    
    0 讨论(0)
  • 2020-12-31 19:07

    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;}
    
    0 讨论(0)
  • 2020-12-31 19:09

    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.");
    }
    
    0 讨论(0)
  • 2020-12-31 19:12

    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;
    
    0 讨论(0)
  • 2020-12-31 19:14

    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");
    }
    
    0 讨论(0)
提交回复
热议问题