Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent special characters in UITextField

How can I prevent the user from entering special characters in UITextField?

like image 305
Mantosh Kumar Avatar asked Mar 12 '23 12:03

Mantosh Kumar


1 Answers

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
    {
    }
}
like image 65
Mantosh Kumar Avatar answered Mar 25 '23 07:03

Mantosh Kumar