Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?    
}
like image 423
ADude Avatar asked Aug 21 '10 20:08

ADude


1 Answers

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]) {
...
}
like image 56
kovpas Avatar answered Sep 22 '22 18:09

kovpas