How to restrict user to enter character in phone UITextField

When you want user to enter only number (like in case of mobile number) then you can restrict using this

1. Put a macro before @implementation   

   #define NUMERIC @"1234567890"


2. textField DelegateMethod   
  
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *unacceptedInput = nil;
    if(textField== self. phoneTextField)
    {
        unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:NUMERIC] invertedSet];
    }
    return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}

Comments

Popular posts from this blog

Hacker Rank problem solution

How to Make REST api call in Objective-C.

Building and Running Python Scripts with Xcode