title: How can I restrict special characters in UITextField except dot and underscores?
body: <p>How can I restrict special characters in a <code>UITextField</code> except dot and underscores? </p>
  
  <p>I have tried the code snippet below, but without luck:</p>
  
  <pre><code>#define ACCEPTABLE_CHARECTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
  
  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {
           NSCharacterSet *acceptedInput = [NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS];
          if (![[string componentsSeparatedByCharactersInSet:acceptedInput] count] &gt; 1){
              NSLog(@"not allowed");
              return NO;
          }
          else{
              return YES;
          }
  }
  </code></pre>