You will need to loop through and return NO to the method when the character is a non-alphanumeric one. So, the code that you have should be like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL canEdit=NO;
NSCharacterSet *myCharSet = [NSCharacterSet alphanumericCharacterSet];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
return NO;
}
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength > 0) {
for (int i = 0; i < [string length]; i++)
{
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c])
{
canEdit=NO;
self.myButton.enabled = NO;
}
else
{
canEdit=YES;
self.myButton.enabled = YES;
}
}
} else self.myButton.enabled = NO;
return (newLength > 50 && canEdit) ? NO : YES;
}