UITextField Should accept number only values

前端 未结 14 1178
有刺的猬
有刺的猬 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:49

    Here is Swift solution:

    In viewDidLoad set the delegate:

    _yourTextField.delegate = self
    
    let _acceptableCharacters = "0123456789."
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
        if (string.characters.count == 0) {
            return true
        }
    
        if (textField == self._yourTextField) {
            let cs = NSCharacterSet(charactersInString: self._acceptableCharacters)
            let filtered = string.componentsSeparatedByCharactersInSet(cs).filter {  !$0.isEmpty }
            let str = filtered.joinWithSeparator("")
    
            return (string != str)
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-12-08 10:50

    Objective C

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (!string.length) 
            return YES;
    
        if (textField == self.tmpTextField)
        {
            NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
            NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
            NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                                   options:NSRegularExpressionCaseInsensitive 
                                                                                     error:nil];
            NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                                options:0
                                                                  range:NSMakeRange(0, [newString length])];        
            if (numberOfMatches == 0)
                return NO;        
        }
        return YES;
    }
    

    Swift 3.0

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if !string.characters.count {
            return true
        }
        do {
            if textField == self.tmpTextField {
                var newString = textField.text.replacingCharacters(inRange: range, with: string)
                var expression = "^([0-9]+)?(\\.([0-9]{1,2})?)?$"
                var regex = try NSRegularExpression(pattern: expression, options: NSRegularExpressionCaseInsensitive)
                var numberOfMatches = regex.numberOfMatches(inString: newString, options: [], range: NSRange(location: 0, length: newString.characters.count))
                if numberOfMatches == 0 {
                    return false
                }
            }
        }
        catch let error {
        }
        return true
    }
    
    0 讨论(0)
提交回复
热议问题