How to Limit the Number of Decimal Points in a Uitextfield

How to limit the textfield entry to 2 decimal places in swift 4?

Set your controller as the delegate for the text field and check if the proposed string satisfy your requirements:

override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.keyboardType = .decimalPad
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let oldText = textField.text, let r = Range(range, in: oldText) else {
return true
}

let newText = oldText.replacingCharacters(in: r, with: string)
let isNumeric = newText.isEmpty || (Double(newText) != nil)
let numberOfDots = newText.components(separatedBy: ".").count - 1

let numberOfDecimalDigits: Int
if let dotIndex = newText.index(of: ".") {
numberOfDecimalDigits = newText.distance(from: dotIndex, to: newText.endIndex) - 1
} else {
numberOfDecimalDigits = 0
}

return isNumeric && numberOfDots <= 1 && numberOfDecimalDigits <= 2
}

How can I limit the number of decimal points in a UITextField?

Implement the shouldChangeCharactersInRange method like this:

// Only allow one decimal point
// Example assumes ARC - Implement proper memory management if not using.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *arrayOfString = [newString componentsSeparatedByString:@"."];

if ([arrayOfString count] > 2 )
return NO;

return YES;
}

This creates an array of strings split by the decimal point, so if there is more than one decimal point we will have at least 3 elements in the array.

How to limit decimal input value in UITextField

You probably need two checks:

  1. Make sure it in the form of xxx.xx. This sort of pattern matching is often achieved by using regular expression search.

    The trick here is to make sure you support all permutations with and without decimal place, where the fractional digits is two or fewer digits and the integer digits is three or fewer digits.

  2. Try converting it to a number and check that the value is less than 999.

Thus:

let formatter = NumberFormatter()

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let candidate = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
let separator = formatter.decimalSeparator!

if candidate == "" { return true }

let isWellFormatted = candidate.range(of: "^[0-9]{1,3}([\(separator)][0-9]{0,2})?$", options: .regularExpression) != nil

if isWellFormatted,
let value = formatter.number(from: candidate)?.doubleValue,
value >= 0,
value < 999 {
return true
}

return false
}

Note:

  • I’m assuming you want users to be able to honor their device’s localization settings (e.g. let a German user enter 123,45 because they use , as the decimal separator).

  • The regular expression, "^[0-9]{1,3}([\(separator)][0-9]{0,2})?$” probably looks a little hairy if you’re not used to regex.

    • The ^ matches the start of the string;
    • The [0-9] obviously matches any digit;
    • The {1,3} matches between one and three integer digits;
    • The (...)? says “optionally, look for the following”;
    • Again, [0-9]{0,2} means “between zero and two fractional digits; and
    • The $ matches the end of the string.

How to limit textfield decimal

Regular expressions are useful when trying to test to see if a string matches a particular template:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let oldString = textField.text ?? "" as NSString
let candidate = oldString.stringByReplacingCharactersInRange(range, withString: string)
let regex = try? NSRegularExpression(pattern: "^\\d{0,2}(\\.\\d?)?$", options: [])
return regex?.firstMatchInString(candidate, options: [], range: NSRange(location: 0, length: candidate.characters.count)) != nil
}

The \d{0,2} matches zero to two digits. The (\.\d?)? translates to "if there is a decimal point, allow it and optionally one more digit." (You could also do (\.\d{0,1})?, too.) The ^ matches the "start of the string" and the $ matches the "end of the string" (i.e., we'll only find matches where the \d{0,2}(\.\d?)? is the entire string, and not just something that shows up in the middle of the string.) And, of course, all of those \ characters are escaped to be \\ in the string literal.

Regular expressions (regex) can be a little "dense" when you first encounter them, but once you gain familiarity with the syntax, it's an incredibly powerful tool.

--

By the way, I notice that you said in a comment that you're using Swift 1.1. I believe the equivalent Swift 1.1 syntax would be something like:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let oldString = textField.text as NSString
let candidate = oldString.stringByReplacingCharactersInRange(range, withString: string)
var error: NSError?
let regex = NSRegularExpression(pattern: "^\\d{0,2}(\\.\\d?)?$", options: 0, error: &error)
return regex?.firstMatchInString(candidate, options: 0, range: NSRange(location: 0, length: candidate.length)) != nil
}

I don't have a Swift 1.1 compiler readily available, so I can't easily test/confirm this Swift 1.1 syntax, but if I recall correctly, it's something like that. Personally I'd recommend upgrading to the latest version of Xcode, but to each his own.

How to restrict textfield to accept only decimal values in swift

If you want to allow just decimal number with your textField you can simply make it like this way no need to compare anything else.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text != "" || string != "" {
let res = (textField.text ?? "") + string
return Double(res) != nil
}
return true
}

Limit Text Field to one decimal point input, numbers only, and two characters after the decimal place - Swift 3

You need to assign delegate to your textfield and in the shouldChangeCharactersIn delegate method do your validations:

  1. Add extension with validation methods for the string:

    extension String{

    private static let decimalFormatter:NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.allowsFloats = true
    return formatter
    }()

    private var decimalSeparator:String{
    return String.decimalFormatter.decimalSeparator ?? "."
    }

    func isValidDecimal(maximumFractionDigits:Int)->Bool{

    // Depends on you if you consider empty string as valid number
    guard self.isEmpty == false else {
    return true
    }

    // Check if valid decimal
    if let _ = String.decimalFormatter.number(from: self){

    // Get fraction digits part using separator
    let numberComponents = self.components(separatedBy: decimalSeparator)
    let fractionDigits = numberComponents.count == 2 ? numberComponents.last ?? "" : ""
    return fractionDigits.characters.count <= maximumFractionDigits
    }

    return false
    }

    }
  2. In your delegate method:

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

    // Get text
    let currentText = textField.text ?? ""
    let replacementText = (currentText as NSString).replacingCharacters(in: range, with: string)

    // Validate
    return replacementText.isValidDecimal(maximumFractionDigits: 2)

    }

Limiting user input to a valid decimal number in Swift

Here is a simple example:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

self.textField.delegate = self

}

//Textfield delegates
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // return NO to not change text

switch string {
case "0","1","2","3","4","5","6","7","8","9":
return true
case ".":
let array = Array(textField.text)
var decimalCount = 0
for character in array {
if character == "." {
decimalCount++
}
}

if decimalCount == 1 {
return false
} else {
return true
}
default:
let array = Array(string)
if array.count == 0 {
return true
}
return false
}
}
}


Related Topics



Leave a reply



Submit