How to limit the textfield entry to 2 decimal places in swift 4?
Only Two decimal point allow and only one "." allow
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text: NSString = textField.text! as NSString
let resultString = text.replacingCharacters(in: range, with: string)
//Check the specific textField
if textField == costTextField {
let textArray = resultString.components(separatedBy: ".")
if textArray.count > 2 { //Allow only one "."
return false
}
if textArray.count == 2 {
let lastString = textArray.last
if lastString!.count > 2 { //Check number of decimal places
return false
}
}
}
return true
}
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
}
In Swift 4.
TextField have 10 digit limit and after decimal 2 digit limit (you can change the Limits). The dot will allow only one time in the textField.
class ViewController: UIViewController,UITextFieldDelegate {
var dotLocation = Int()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let nonNumberSet = CharacterSet(charactersIn: "0123456789.").inverted
if Int(range.length) == 0 && string.count == 0 {
return true
}
if (string == ".") {
if Int(range.location) == 0 {
return false
}
if dotLocation == 0 {
dotLocation = range.location
return true
} else {
return false
}
}
if range.location == dotLocation && string.count == 0 {
dotLocation = 0
}
if dotLocation > 0 && range.location > dotLocation + 2 {
return false
}
if range.location >= 10 {
if dotLocation >= 10 || string.count == 0 {
return true
} else if range.location > dotLocation + 2 {
return false
}
var newValue = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
newValue = newValue?.components(separatedBy: nonNumberSet).joined(separator: "")
textField.text = newValue
return false
} else {
return true
}
}
}
Guys, here's the solution:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let dotString = "."
if let text = textField.text {
let isDeleteKey = string.isEmpty
if !isDeleteKey {
if text.contains(dotString) {
if text.components(separatedBy: dotString)[1].count == 2 {
return false
}
}
}
}
}