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.
Asked
Active
Viewed 1,228 times
-1

Johann Blais
- 9,389
- 6
- 45
- 65

user3291536
- 29
- 1
- 1
- 5
4 Answers
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);
}
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
-
thanks for Replay but This code number also allowed – user3291536 Feb 10 '14 at 07:07
-
1. Did you connect your UITextField with IBOutlet? 2. Did you set the UITextFieldDelegate to your textfield? – GenieWanted Feb 10 '14 at 07:09
-
Sir, downvoter.. care to explain why the downvote? :/ – GenieWanted Feb 10 '14 at 07:36
-1
try this
NSCharacterSet *blockedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
return ([characters rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);
}

Kuldeepsingh Rajput
- 23
- 1
- 7
-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

Naresh Chouhan
- 31
- 3