Friday, October 26, 2012

How to restrict user to enter character in phone text field


How to restrict user to enter character in phone text field

Get contacts of all your emergency needs on single tap. Download Quick-Finder for iPhone

 1) Take a text field let its name is phoneTextField.


2) attach delegate of phoneTextField to FileOwner.
                          Or
    you can do it by code as


self. phoneTextField.delegate=self;

3) implement the delegate UITextFieldDelegate in .h file.

4) Put a macro before @implementation  


    #define NUMERIC                 @"1234567890"

5) implement the following method as:


- (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);}





1 comment: