textField限制輸入整數0-100

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSString *regexStr = @"^\\d{0,3}$";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@", regexStr];
    if([predicate evaluateWithObject:text] && [text integerValue] <= 100){
        return YES;
    }
    return NO;
    
}
相關文章
相關標籤/搜索