I have a first name validation to be done in swift4 which is in a UITextField
. The thing is that:
- The Firstname should start with a letter and end with a letter,
- No trailing and leading whitespace.
- It can have spaces between them, can contain dot.
- should be between 7 and 18 characters.
Currently i am having the following validation in the UITextField
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let STRING_ACCEPTABLE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
switch textField {
case firstName:
print("firstname")
if string == " " {
return false
} else {
let cs = NSCharacterSet(charactersIn: STRING_ACCEPTABLE_CHARACTERS).inverted
let filtered = string.components(separatedBy: cs).joined(separator: "")
return (string == filtered)
}
}
return true
}
Any idea how to implement the following conditions. Right now I am disabling the space and only accepts the alphabets.
Any help will be very much appreciated