Hello,
This is my solution.
What do you think about?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let charSet = CharacterSet.letters
let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
let replacementTextHasDecimalSeparator = string.range(of: ".")
let existingTextHasAlphabeticCharacters = textField.text?.rangeOfCharacter(from: charSet)
let replacementTextHasAlphabeticCharacters = string.rangeOfCharacter(from: charSet)
if existingTextHasDecimalSeparator != nil,
replacementTextHasDecimalSeparator != nil {
return false
} else if existingTextHasAlphabeticCharacters != nil ||
replacementTextHasAlphabeticCharacters != nil {
return false
} else {
return true
}
}
Thanks in advance.