0

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];
}
Community
  • 1
  • 1
daspianist
  • 4,972
  • 8
  • 46
  • 87
  • What are special characters ? That's very ambiguous. – uchuugaka Feb 03 '14 at 23:13
  • Special characters are non alphanumeric characters like "!", "#", "~", and etc. Since my goal is to limit these special characters in the creation of usernames, I thought it was ok to not specifically identify what these special characters are. – daspianist Feb 04 '14 at 00:11

2 Answers2

2

Use the solution from the other question but instead of building the character set from the fixed letters, use the standard NSCharacterSet alphanumericCharacterSet.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • Thanks for the suggestion. I updated the question with the code I am using, but get the error `NSInvalidArgumentException', reason: '-[__NSCFCharacterSet length]:` when I set it to `NSCharacterSet alphanumericCharacterSet` – daspianist Feb 03 '14 at 20:53
  • 1
    You're using `characterSetWithCharactersInString:` and passing a character set (not a string). Get rid of your outer NSCharacterSet. – Rob Napier Feb 03 '14 at 20:55
0

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.

Rychu
  • 810
  • 1
  • 7
  • 17