How can I prevent the user from entering special characters in UITextField
?
I solved the problem using this code:
let validString = NSCharacterSet(charactersInString: " !@#$%^&*()_+{}[]|\"<>,.~`/:;?-=\\¥'£•¢")
// restrict special char in test field
if (textField == self.txt_firstName || textField == self.txt_lastName)
{
if let range = string.rangeOfCharacterFromSet(validString)
{
print(range)
return false
}
else
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With