Saturday 16 February 2013

How to restrict alpha character in phone UITextField i.e only enter numeric character(0,1,2...9)

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);
}
Note:Don't forgot connect textField delegate to fileowner

No comments:

Post a Comment