title: TextField Validation With Regular Expression
body: <p>I need help with code that looks at a textfield make sure it starts with either a (+ or -) then has 3 integers after it. </p>
  
  <p>So valid data looks like +234 or -888</p>
  
  <p>So I have started this code but there are 2 problems with it </p>
  
  <ol>
  <li><p>It correctly validates that only 4 characters are entered. But for some reason you have to take focus off the textfield in order for the Done button on the keyboard to fire and hide the keyboard. If I only put less than 4 characters in the textfield then the Done button works fine. But I dont want the user to enter anything but 4 characters and then press Done and hide the keyboard. Thats the first problem....</p></li>
  <li><p>I am not familar with regular expressions and how to use them in iphone. So I need to add to this code regular expression for the above requirement.</p>
  
  <pre><code>-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {
       NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
       return !([newString length] &gt; 4);
  }
  //Done button to hide the keyboard
  -(IBAction)Done:(id)sender
  {
  
  }
  </code></pre></li>
  </ol>