iOS金額輸入限制

weixin_33895657發表於2018-12-18

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length ==10) {
        if ([string isEqualToString:@""]) {
            return YES;
        }
        return NO;
    }
    if (textField == self.moneyField) {
        if ([textField.text rangeOfString:@"."].location==NSNotFound) {
            _isContainsPoint = NO;
        }
        if ([textField.text rangeOfString:@"0"].location==NSNotFound) {
            _isFirstZero = NO;
        }
       
        if ([string length]>0){
            unichar single=[string characterAtIndex:0];//當前輸入的字元
            if ((single >='0' && single<='9') || single=='.')//資料格式正確
            {
               
                if([textField.text length]==0){
                    if(single == '.'){
                        textField.text = @"0";
                        _isContainsPoint = YES;
                        //首字母不能為小數點
                        return YES;
                    }
                    if (single == '0') {
                        _isFirstZero = YES;
                        return YES;
                    }
                }
               
                if (single=='.'){
                    if(!_isContainsPoint)//text中還沒有小數點
                    {
                        _isContainsPoint=YES;
                        return YES;
                    }else{
                        return NO;
                    }
                   
                }else if(single=='0'){
                    if ((_isFirstZero&&_isContainsPoint)||(!_isFirstZero&&_isContainsPoint)) {
                        //首位有0有.(0.01)或首位沒0有.(10200.00)可輸入兩位數的0
                        if([textField.text isEqualToString:@"0.0"]){
                            return NO;
                        }
                        //前方不可插入0  00.11返回NO
                        if ([string isEqualToString:@"0"]) {
                            if (range.location == 0) {
                                return NO;
                            }else if (_isFirstZero&&_isContainsPoint){
                                if (range.location == 1) {
                                    return NO;
                                }
                            }
                        }
                       
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt=(int)(range.location-ran.location);
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }else if (_isFirstZero&&!_isContainsPoint){
                        //首位有0沒.不能再輸入0
                        return NO;
                    }else{
                        return YES;
                    }
                }else{
                    if (_isContainsPoint){
                        //存在小數點,保留兩位小數
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt= (int)(range.location-ran.location);
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }else if(_isFirstZero&&!_isContainsPoint){
                        //首位有0沒點
                        return NO;
                    }else{
                        return YES;
                    }
                }
            }else{
                //輸入的資料格式不正確
                return NO;
            }
        }else{
            return YES;
        }
    }
    return YES;
}

相關文章