views:

52

answers:

1

Hi

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 his just tells me that now characters were entered which aren't number 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 ?    
}
+1  A: 

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]) {
...
}
kovpas
Thanks you. Yes this is what I ended up doing in the end.
ADude