Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string only contains alphanumeric characters in objective C?

I'm working on a small iphone project and i would need to check if the userName entered only contains alphanumerical characters? (A-Z, a-z, 0-9. How would i go about checking it?

like image 935
Cheng Lai Avatar asked Nov 04 '09 03:11

Cheng Lai


People also ask

How do you know if a string contains alphanumeric?

Python String isalnum() Method The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!

How do I check if a string contains a character in Objective C?

To check if a string contains another string in objective-c, we can use the rangeOfString: instance method where it returns the {NSNotFound, 0} if a 'searchString' is not found or empty (""). Output: string contains you!


1 Answers

If you don't want to bring in a regex library for this one task...

NSString *str = @"aA09"; NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet]; BOOL valid = [[str stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];  
like image 147
Jason Harwig Avatar answered Oct 08 '22 11:10

Jason Harwig