开发者

Validate file name(or folder name) in iPhone

开发者 https://www.devze.com 2023-04-04 23:05 出处:网络
I am trying to validate a file n开发者_高级运维ame/ folder name when user try to change the name.

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");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号