Validate file name(or folder name) in iPhone
I am trying to validate a file n开发者_高级运维ame/ folder name when user try to change the name. I know I should use Regex/ NSPredicate. However, I didn't figure out after hours researching. Appreciate if any experts can help me out. Thanks a lot.
Was suggested by Madhumal Gunetileke: I revised his code as below. It works for my case.
NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"\\/:*?\"<>|"];
if ([newName rangeOfCharacterFromSet:set].location != NSNotFound) {
NSLog(@"illegal Name");
return;
}
If you're simply looking out to check whether the new name users provide are valid or not, you could try something like this;
NSString *yourString = @"<the name you want to verify comes here>";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];
if ([yourString rangeOfCharacterFromSet:set].location != NSNotFound) {
NSLog(@"NOT Legal");
}
else {
NSLog(@"Legal");
}
精彩评论