1

I have a tableview where the last row is UITextField and where the user is supposed to type a first and last name.

What I would like to do is to remove or restrict any whitespace before the first name (first word). Next, I would like to allow the user to use whitespace. How can I do that?

Thank you in advance.

Adel
  • 411
  • 5
  • 9
  • http://stackoverflow.com/a/4645698/1415949 At that link is a VERY simple method for doing this, which has been discussed on here many, many times :) – anon_dev1234 Mar 02 '13 at 19:20
  • I have read all these questions and tried suggested answers before posting my question. This tells me that the problem is might be somewhere else. Thank you. – Adel Mar 02 '13 at 19:29
  • by the way I have updated the question. – Adel Mar 02 '13 at 19:32
  • Adel: So you don't want to remove it really. You want to keep the user from entering a whitespace if they haven't entered actual text yet. Live. – Josiah Mar 02 '13 at 19:36
  • It's still the same thing. What I've linked can just be called every time the user enters a character in that case. It only strips the left and right sides of whitespace, so whether you call it afterwards when you have a full string, or call it as you go, you can still use it for validating. Just trim all whitespace and replace. – anon_dev1234 Mar 02 '13 at 19:50
  • So, stringByTrimmingCharactersInSet is not working for this? – user523234 Mar 02 '13 at 19:55

1 Answers1

1

Probably you should implement a method that listen to text change in UITextField. using

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

So that you should be knowing whenever user enter a charachter in UITextFiled from that you can check if user has entered white space delete that and prompt user to write again.

Here is how you can delete last charecter that user have entered whenever textFieldDidChange: is called and you checked that user has entered white space

if ( [myString length] > 0)
{
     myString = [myString substringToIndex:[string length] - 1];
}
abdus.me
  • 1,819
  • 22
  • 34