I have a textfield and I want to limit the entry to max 2 decimal places.
number like 12.34 is allowed but not 12.345
How do I do it?
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
}