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.
NSString *string = [NSString stringWithString:nameField.text];
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
[string stringByTrimmingCharactersInSet:alphaSet];
[string stringByReplacingOccurrencesOfString:@"_" withString:@""];
BOOL valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];
You can create your own character set:
Once you have that, you invert it to everything that’s not in your original string:
And you can then use a string method to find if your string contains anything in the inverted set: