简体   繁体   English

如何禁止特殊字符但允许在UITextField中使用亚洲/中东字符

[英]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. 我正在使用UITextField为用户输入用户名,并希望限制除句点和下划线之外的特殊字符。 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. 我最初开始使用这个SO问题解决方案 ,直到我意识到我不想仅限于字母数字字符,而且还允许亚洲和中东语言字符。 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: 根据rmaddy的建议,这是我目前使用的:

- (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? 也许在循环中手动测试字符串中的unicode字符对包含你需要的unicode集的范围? 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. 我做了快速检查,似乎unicode套日语字母通常密集,除了一些特殊字符 - 我在这里看了http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml但我猜测类似也应该对其他语言有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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