Check if NSString contains alphanumeric + underscore characters only

后端 未结 7 1747
余生分开走
余生分开走 2021-01-30 00:52

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         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 01:18

    this is a quick helper if it helps

    -(BOOL)isString:(NSString *)s{
        char letter = 'A';
        //BOOL isLetter=NO;
        for (int i=0; i<26; i++) {
            NSString *temp = [NSString stringWithFormat:@"%c",letter+i];
            if ([s isEqualToString:temp]) {
                return YES;
            }
        }
        return NO;
    }
    

提交回复
热议问题