0

I am trying to add some validation to amount in textField.I am trying to allow user to add valid numbers using Decimal Keypad. User can add numbers less than 100000 (99999.99,9999,9,1) User cant add numbers like [12.. , 12.0.2 , 12.345 , 100.23456

Here is what i tried. if i try to add max length to 5.i could not be able to add 99999.99. If i add max length to 8. i can able to add numbers more than 99999. Hope u understand my problem.Thanks in advance

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let maxLength = 5
    var newString : NSString = ""

    if(textField.tag == 3){
        let currentString: NSString = tvAmount.text! as NSString
        newString  =
            currentString.replacingCharacters(in: range, with: string) as NSString

        // max 2 fractional digits allowed
        let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let regex = try! NSRegularExpression(pattern: "\\..{3,}", options: [])
        let matches = regex.matches(in: newText, options:[], range:NSMakeRange(0, newText.characters.count))
        guard matches.count == 0 else { return false }

        if(newString.length < 5){
            switch string {
            case "0","1","2","3","4","5","6","7","8","9":
                return true
            case ".":
                let array = textField.text?.characters.map { String($0) }
                var decimalCount = 0
                for character in array! {
                    if character == "." {
                        decimalCount = decimalCount + 1
                    }
                }

                if decimalCount == 1 {
                    return false
                } else {
                    return true
                }
            default:
                let array = string.characters.map { String($0) }
                if array.count == 0 {
                    return true
                }
                return false
            }
        }
        else{
        }
    }
    return newString.length <= maxLength
}
John
  • 3,769
  • 4
  • 12
  • 23
  • there are also a lot of other relevant questions with answers under the related questions tab on this page :) e.g. also https://stackoverflow.com/questions/27215495/limit-uitextfield-input-to-numbers-in-swift?rq=1 – Daij-Djan May 22 '18 at 05:18
  • I think u didnt understand my problem. I want to validate the number of characters in amount. For example 99999.99 - 8 characters 99999 - 5 characters – John May 22 '18 at 05:28
  • Daij .if it is integer number i want to limit character to 5 digits.if it is Decimal number i want to limit character to 8 digits. – John May 22 '18 at 05:36
  • limit input and just check if the input is < 100000 – Daij-Djan May 22 '18 at 05:37
  • This regular expression will help you "^[0-9]{0,5}((\\.|,)[0-9]{0,2})?$" . "0-5" 5 is the decimal limit and you can set your own limit. "0-2" 2 is the fractional limit. – Mitesh Dobareeya May 22 '18 at 06:26
  • https://stackoverflow.com/questions/10404067/how-can-i-limit-the-number-of-decimal-points-in-a-uitextfield/10405855?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Mitesh Dobareeya May 22 '18 at 06:27
  • Thanks a lot Mitesh.This is what i expected – John May 22 '18 at 06:47

0 Answers0