I'm able to limit the length using the code below, but I can't seem to find a way to also limit special characters.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
if (textField.placeholder == "USERNAME")
{
//Also limit special characters here
return newLength <= 15 // Bool
}
characters I want allowed:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.
I tried following this link but its in objective C and I'm having trouble merging it with my current 15 character limit code above