简体   繁体   中英

How to disallow special characters but allow Asian/Middle Eastern characters in a UITextField

I am using a UITextField for users to enter usernames, and would like to restrict special characters except for periods and underscores. I was initially set on using the solution from this SO question , until I realized that I do not want to restrict to only alpha-numeric characters, but also allow Asian and Middle Eastern languages characters as well. Is there a way that I would be able to accomplish this?

Thanks!

Update:

Per rmaddy's suggestion, here is what I am presently using:

- (BOOL)userNameIsAcceptable: (NSString *)userNameInputted
{
    NSCharacterSet *userNameAcceptedInput = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    NSString *filteredUserName = [[userNameInputted componentsSeparatedByCharactersInSet:userNameAcceptedInput] componentsJoinedByString:@""];
    NSLog(@"The filtered result %@", filteredUserName);

    return [userNameAcceptedInput isEqual:filteredUserName];
}

使用其他问题的解决方案,但不使用固定字母构建字符集,而是使用标准NSCharacterSet alphanumericCharacterSet

Maybe testing unicode chars from strings manually in loop against ranges containing unicode sets you need? I did quick check, and it seems that unicode sets of Japanese letters are usually densely packed, except some special characters - I've looked here http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml but I guess similar should be valid to other languages as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM