sagarkothari
sagarkothari

Reputation: 24810

only character - not special/numbers from keyboard - iPhone

Normal UITextField displays a UIKeyboard which includes alpha+Numberic+specials

But I need alpha only...

I need this because if user enters something like -- A'bad

it has a char ' -- which is affecting my query .

How we can set only alphabetic UIKeyboard to user?

Upvotes: 2

Views: 3252

Answers (4)

Rupesh
Rupesh

Reputation: 2051

Instead of disabling or not allowing user to add '(apostorpe) use following code, it will allow you to insert '(apostorpe)

[textEntered stringByReplacingOccurrencesOfString:@" ' " withString:@" ' '"];

Upvotes: 0

alicanbatur
alicanbatur

Reputation: 2200

You should use as i do below;

- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    NSMutableCharacterSet *filteredChars = [NSMutableCharacterSet letterCharacterSet];
    [filteredChars formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
    NSCharacterSet *blockedCharSet = [filteredChars invertedSet];
    if (([string rangeOfCharacterFromSet:blockedCharSet].location == NSNotFound)) {
          return YES;
    }
    return NO;
}

Upvotes: 0

sagarkothari
sagarkothari

Reputation: 24810

I think. I got the answer.

In your UIViewController file .h add delegate<UITextfieldDelegate>

Now add following code to .m file

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

    for (int i = 0; i < [textEntered length]; i++) {
        unichar c = [textEntered characterAtIndex:i];
        if (![myCharSet characterIsMember:c]) {
           return NO;
        }
    }
    return YES;

}

Upvotes: 2

Boon
Boon

Reputation: 41470

This SO post answers your question: UIkeyboard type

Upvotes: 1

Related Questions