Tuesday, 16 October 2012

iPhone Development:Restrict character to be entered in UITextField



in iPhone application development, if you want to restrict users from entering unnecessary characters in text field then you can use the facility provide by NSCharcterSet. Below is the code snippets that allow user to enter only numeric characters into text-field.No other characters apart from numeric values will be inserted in text field.

- (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string 
{
static NSCharacterSet *charSet3 = nil;
   if(!charSet3) 
    {
        charSet3 = [[NSCharacterSet
       characterSetWithCharactersInString:@"0123456789 "] 
                      invertedSet];
    } 
    NSRange location = [string rangeOfCharacterFromSet:charSet3];
    return (location.location == NSNotFound);
}

No comments:
Write comments