-1

I have created a UITextField and I want to give validations for the textfield(userName) such that only characters are allowed. If the user enters anything else other than a alphabet like numbers or special characters, it shouldn't appear and also there should be a notification to show the same.

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
user3291536
  • 29
  • 1
  • 1
  • 5

4 Answers4

3
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

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


    NSCharacterSet *blockedCharacters = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];

    if (!([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound)) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Only allow alphabet." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
    }

    return ([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);

}
emenegro
  • 6,901
  • 10
  • 45
  • 68
FARAZ
  • 645
  • 7
  • 6
0

You can try this:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if(textField==YOUR_TEXTFIELD_NAME)
        {
            NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
            for (int i = 0; i < [string length]; i++) {
                unichar c = [string characterAtIndex:i];
                if (![myCharSet characterIsMember:c]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Numbers not allowed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    return NO;
                }

            }

    }
return YES;
}

Make sure you set the delegate for your textfield.

GenieWanted
  • 4,473
  • 4
  • 24
  • 35
-1
try this 

NSCharacterSet *blockedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    return ([characters rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);
}
-1
#define ACCEPTABLE_NUMBER @"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

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

        NSCharacterSet *acceptedInput = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_NUMBER]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:acceptedInput] componentsJoinedByString:@""];
        if ((![filtered isEqualToString:string]))
            return NO;
    }

}
Charles
  • 50,943
  • 13
  • 104
  • 142