ignasibm
ignasibm

Reputation: 25

CharacterSet containing all variation of letters

I'm working on a registration form where the user has to enter their full name, meaning that the only acceptable characters for this specific textfield should be (A-Z, a-z) plus " " (space) but no numbers, nor special caracter such -`+¨¨_{ etc. It should also accept accents, such as: [à, é, û] and all other combinations for all letters.

What I have right now is:

- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters {

    NSMutableCharacterSet *mySet = [NSMutableCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZñ. "];

    return ([characters rangeOfCharacterFromSet:[mySet invertedSet]].location == NSNotFound);
}

Which works fine but doesn't include the accented characters that I need to support, and I'm sure there should be simpler way of including the missing characters without having to type them one at a time.

Upvotes: 0

Views: 49

Answers (1)

Sulthan
Sulthan

Reputation: 130102

Why don't you use the specific character set with all letters, including all diacritics?

NSMutableCharacterSet *mySet = [[NSCharacterSet letterCharacterSet] mutableCopy];
[mySet addCharactersInString:@" "];

return ([characters rangeOfCharacterFromSet:[mySet invertedSet]].location == NSNotFound);

This will not only cover all diacritics, it will also cover all non-english letters, e.g. the Russian alphabet.

Upvotes: 2

Related Questions