IOS UITextField Should accept number only values

[textField setKeyboardType:UIKeyboardTypeNumberPad];
or
this is the function which checks for the String contains Numeric value only
+(BOOL) checkforNumeric:(NSString*) str
{
    NSString *strMatchstring=@"\\b([0-9%_.+\\-]+)\\b"; 
    NSPredicate *textpredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",
                                                               strMatchstring];

    if(![textpredicate evaluateWithObject:str])
    {
        //////NSLog(@"Invalid email address found");
        UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:APP_NAME 
                                message:@"please enter valid text." 
                                delegate:nil cancelButtonTitle:nil 
                                otherButtonTitles:@"Close",nil];
        [objAlert show];
        [objAlert release];
        return NO;
    }
    return YES;
}
or 
There are multiple ways you can do this..  one suggested by Dautermann 
and the other one you can implement this delegate method... 
 
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:
                                                   @"0123456789"] invertedSet];

    if ([textField.text rangeOfCharacterFromSet:set].location != NSNotFound) {

        //show alert view here

        textField.text=@"";
    }

} 
 
 

Comments

Popular posts from this blog

Java : Variables Declaring

SQL Self JOIN