UITextField Should accept number only values

前端 未结 14 1243
有刺的猬
有刺的猬 2020-12-08 09:50

I have UITexfields i want that it should accept only number other shows alert that enter a numeric value. I want that motionSicknessTextFiled should only accept number

14条回答
  •  眼角桃花
    2020-12-08 10:27

    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 FALSE;
        }
        return TRUE;
    }
    

    check it on submit button.

提交回复
热议问题