0

I have searched for an answer but cant find an answer related to this.

I am trying to validate every input into a textfield but not sure how to go about it, the idea is to check a username exist on a server so I would assume I need to post to the server each time something is entered and if success do whatever.

Could someone please advise I would be grateful.

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
SirL0fty
  • 55
  • 1
  • 8
  • Check this link: http://stackoverflow.com/questions/5765654/textfield-validation-with-regular-expression. Really impressive answer.. It helped me.Gud luck – Meet Feb 13 '12 at 04:42

3 Answers3

3

Implement the delegate method textField:shouldChangeCharactersInRange:replacementString: to be informed about every change to the text. Return YES or NO depending on whether you want to accept the change or not.

It is probably a very bad idea to ask the server for approval of every single key press since that would create an intolerable lag and lead to a very bad user experience.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Hi Ole Thanks for your reply, could you provide some sort of example for me please im quite new to all of this. Thanks – SirL0fty Sep 25 '11 at 19:39
  • No, I won't write the code for you since I don't think that's good for learning. The documentation for `UITextField` and the method I listed above should help you. – Ole Begemann Sep 25 '11 at 19:42
2

What Ole is saying is absolutely correct. I'll just give you an example of what he's saying.

First conform your class to the UITextFieldDelegate protocol. Then make sure in viewDidLoad (or similar) that you set the delegate to your class:

myTextField.delegate = self;

Next you can implement the method that Ole was mentioning. However, in your case you're not validating whether the user can enter anything or not, you just want to know what he entered so you can validate it online.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    [self verifyTheUserWithUsername:[textField.text stringByReplacingCharactersInRange:range withString:string]];
    //do your appropriate check here
    return YES; //we allow the user to enter anything
}
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • `[self verifyTheUserWithUsername:textField.text];` doesn't work very well in `shouldChangeCharactersInRange:` method, since it informs of an edit before it's been applied to the textfield. – alex-i Sep 25 '11 at 19:51
  • @SirL0fty: I would definately go with the suggestions mentioned by Ole and babbidi. Checking the server after each key press will really bog your app down. Starting a timer and invalidating it if another key is pressed would definitely be the way to go. I'd suggest something like one second as the interval for a check. – sudo rm -rf Sep 25 '11 at 19:55
  • 1
    @babbidi you could do something like this:'[self verifyTheUserWithUsername:[textField.text stringByAppendingString:string]];'since the paramter 'string' is the new input typed by the user. – Mat Sep 25 '11 at 21:35
  • @Mat: Thanks for mentioning that, I forgot that the textfield's string hasn't been updated before this method is called. I'll edit my answer for future reference. – sudo rm -rf Sep 25 '11 at 21:36
  • Maybe you have to comment the NSString *string=textField.text;? – Mat Sep 25 '11 at 21:39
  • 1
    actually you should use `[textField.text stringByReplacingCharactersInRange:range withString:string]` because it handles inserts in the middle of the string and replacements of parts of the string too – Matthias Bauch Sep 25 '11 at 22:02
2

You can use addTarget:action:forControlEvents: with UIControlEventValueChanged (e.g: [textfield addTarget:self action:@selector(usernameChanged:) forControlEvents:UIControlEventValueChanged). This will inform you AFTER the text changes (so when you receive usernameChanged: you'll have the last modifications).

It's probably a good idea that instead of sending a request to the server each time the username changes you either send a request when the textfield looses caption or when some time passes from the last change (you can use a NSTimer for this).

You should use a single asynchronous request. If the request is active and the textfield is edited again you should cancel the request and set it to nil (since there's no reason to check the old username). If the request succeeds show up an icon, or whatever you want to do when you get the result from the server.

Edit: Not sure if UIControlEventValueChanged worked on previous versions of iOS or if it was a mistake from my side, but it doesn't seem to work any longer. UIControlEventEditingChanged should be used instead.

alex-i
  • 5,406
  • 2
  • 36
  • 56