2

Is it possible to prevent the user to enter certain characters on search bar while typing. For example, the user types some string... "This is a text " and after word text the user tries enter an '*' character. I wish to prevent the user from entering that character.

Is it possible to restrict the user to enter a certain character while typing on a search bar?

tech_human
  • 6,592
  • 16
  • 65
  • 107

1 Answers1

2

UISearchBar has a delegate much like the one for UITextField. Implement the delegate's searchBar:shouldChangeTextInRange:replacementText: method. Return YES or NO as needed. If you return NO, the text won't be entered.

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // check the resulting text. Return NO if not allowed
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    I will try this out. But does this stop entering the entire text? I just want to stop the user for a character, but he should be able to enter/use the text entered prior to that character. I will play around with this method to see how it works. – tech_human Oct 12 '13 at 00:24
  • 1
    I will try this. But when i used this delegate method searchBarSearchButtonClicked(searchBar: UISearchBar) was not called when clicking in keyboard search button. How to solve this?? – Ramakrishna Jun 06 '16 at 12:51