0

In my app I have a textfield in which users have to enter a string from 1-25 characters. The problem I'm having is stopping them from only entering spaces. I want them to be able to use spaces but only if they have other characters (abc, 1-9, etc.). How can I stop them from only entering spaces?

1

3 Answers 3

2

In your view controller set delegate of UITextField to self. Then implement following method.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {

  if([[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
      return NO;
   }

  return YES;
}
1

Sounds like this question might help.

Just trim the whitespace off the string and then check to see if it is the empty string.

0

Perform a whitespace "trim" on their input and check to see if the result matches an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.