UITextField - prevent the user from entering non-standard characters (for example, emoticons)

My application creates and stores EKEvents

in a standard calendar database, however I found that if I use the emoji as "notes" for an event, it crashes when I try to read the event from Calendar.

I do not want or do not need to use emoticons in event.notes

. I just experienced trials and was shocked when he allowed me to save this event. (Emoticons even appear on the calendar itself)

So, I think my problem can be solved in one of two ways, neither of which I have been able to figure out.

1) Is it possible to disable or hide a custom button on the keyboard? <- I would prefer this solution

enter image description here

2) If not, how do I create a character set that includes all possible characters on all standard keyboards to check when the user is not typing an emoji?

I've tried using some standard character sets for validation, for example alphanumericCharacterSet

, but using that, I can't enter spaces.

Here's the code I'm using so far to check for a character that the user enters:

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (string.length == 0) {
        return YES;
    }

    NSCharacterSet *myCharSet = [NSCharacterSet alphanumericCharacterSet];
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            return YES;
        }
    }

    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Invalid Input" message:@"Oops! You can't use that character." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [av show];
    return NO;
}

      

EDIT: Clarifying the error

No error, just an accident. My application saves calendar events and displays them in a list to the user. The user can then mark the event as Paid or Unpaid, after which I add a line to the .notes

Paid property of the event. This happens when I reload the table, if the event is paid, it displays a specific icon to the user. It crashes when the saved one even has an emoticon as a property .notes

and I try to add .notes

.

+3


source to share


1 answer


You can combine NSCharacterSet

using concatenation to add more valid characters:

    NSMutableCharacterSet *myCharSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
    [myCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];

      

Here is a list of the available character sets to choose from:

controlCharacterSet
whitespaceCharacterSet
whitespaceAndNewlineCharacterSet
decimalDigitCharacterSet
letterCharacterSet
lowercaseLetterCharacterSet
uppercaseLetterCharacterSet
nonBaseCharacterSet
alphanumericCharacterSet
decomposableCharacterSet
illegalCharacterSet
punctuationCharacterSet
capitalizedLetterCharacterSet
symbolCharacterSet
newlineCharacterSet

      



In addition, your code checks to make sure that any character is not outside the specified one; I think you intend to check that the characters are not all outside the set. (This only applies when the user types more than one character at the same time - consider copy and paste).

Think about how to set up your loop to look like this:

NSCharacterSet *myCharSet = [NSCharacterSet alphanumericCharacterSet];
for (int i = 0; i < [string length]; i++) {
    unichar c = [string characterAtIndex:i];
    if ([myCharSet characterIsMember:c] == NO) {
        // add your user alert here
        return NO;
    }
}

      

This way, your loop won't just exit at the first good character it finds; instead, it will exit the first bad character.

+6


source







All Articles