ADude
ADude

Reputation: 345

Check NSString Contains Both Letters And Numbers

I'm looking for the best way to check that an NSString contains both numerical and alphabetical characters. What I have come up with so far is the code below but this just tells me that no characters were entered which aren't numbers or letters.

if( [[myNSString stringByTrimmingCharactersInSet:
            [NSCharacterSet alphanumericCharacterSet]] isEqualToString:@""]){    
  //The string only contains alpha or numerical characters.
  //But now I want to check that both character sets are present ?    
}

Upvotes: 7

Views: 5071

Answers (3)

Monolo
Monolo

Reputation: 18253

I am not sure the two previous answers cover the requirements, so this is an answer corresponding to a stricter interpretation of the requirements.

Taking the requirements from the comments in the code of the original post:

//The string only contains alpha or numerical characters.   
//But now I want to check that both character sets are present?

This means that a string to pass the test must consist of only numbers and letters and at least one of each.

NSString's rangeOfCharacterFromSet: finds a character in a string if it belongs to a certain set, so we can use that one.

NSString *string = // ... string to test;

NSCharacterSet *illegalChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

// Test if there is an illegal character in the string
BOOL hasIllegalCharacter = [string rangeOfCharacterFromSet: illegalCharacterSet].location != NSNotFound;

BOOL hasLetter = [string rangeOfCharacterFromSet: [NSCharacterSet letterCharacterSet]].location != NSNotFound;

BOOL hasDigit  = [string rangeOfCharacterFromSet: [NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;

if (hasDigit  && hasLetter  && !hasIllegalCharacter  ) {
    // String OK
}

As detailed in the docs, the rangeOfCharacterFromSet: returns an NSRange of {NSNotFound, 0} in case no character from the set is found in the string. This is similar to other methods to search for strings or characters in a string in Cocoa.

Upvotes: 4

Amory
Amory

Reputation: 311

kovpas' answer works fine if you have letters at the beginning or end and digits on the opposite end. A problem occurs if digits are embedded between letters or vice-versa, such as 'abc123abc'. Here is a solution:

if (![[[str componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] isEqualToString:str]  
    && ![[[str componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""] isEqualToString:str]) {  
    ....  
}

Upvotes: 6

kovpas
kovpas

Reputation: 9593

Just trim letterCharacterSet and decimalDigitCharacterSet and check if produced string is not equal to the original string:

if (![[myOriginalString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]] isEqualToString:myOriginalString] 
     && ![[myOriginalString stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]] isEqualToString:myOriginalString]) {
...
}

Upvotes: 14

Related Questions