I\'ve got a string that needs to be only a-z, 0-9 and _
How do I check if the input is valid? I\'ve tried this but it accepts letter like å,ä,ö,ø etc.
NS
Some more easy way,
NSMutableCharacterSet *allowedSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"_"];
[allowedSet formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
NSCharacterSet *forbiddenSet = [allowedSet invertedSet];
It'll combine alphanumeric along with _underscore.
you can use it like,
NSRange r = [string rangeOfCharacterFromSet:forbiddenSet];
if (r.location != NSNotFound) {
NSLog(@"the string contains illegal characters");
}
PS. example copied from @DaveDeLong example :)