3

What is the value of a UITextField when it is empty? I can't seem to get this right.

I've tried (where `phraseBox' it the name of the said UITextField

if(phraseBox.text != @""){

and

if(phraseBox.text != nil){

What am I missing?

Maksim
  • 2,054
  • 3
  • 17
  • 33
Moshe
  • 57,511
  • 78
  • 272
  • 425

7 Answers7

21
// Check to see if it's blank
if([phraseBox.text isEqualToString:@""]) {
  // There's no text in the box.
}

// Check to see if it's NOT blank
if(![phraseBox.text isEqualToString:@""]) {
  // There's text in the box.
}
Mike Wheaton
  • 492
  • 4
  • 15
Raphael Caixeta
  • 7,808
  • 9
  • 51
  • 76
13

found this at apple discussions when searching for the same thing,thought ill post it here too. check the length of the string :

NSString *value = textField.text;
if([value length] == 0) {

}

or optionally trim whitespaces from it before validation,so user cannot enter spaces instead.works well for usernames.

NSString *value = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

if([value length] == 0) {
// Alert the user they forgot something
}
vishesh
  • 131
  • 1
  • 2
1
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

  NSString *fullText = [textField.text stringByAppendingString:string];      
  if ((range.location == 0) && [self isABackSpace:string]) {
    //the textFiled will be empty
  }
  return YES;
}

-(BOOL)isABackSpace:(NSString*)string {
  NSString* check =@"Check";
  check = [check stringByAppendingString:string];
  if ([check isEqualToString:@"Check"]) {
    return YES;
  }
  return NO;
}
Marco A.
  • 43,032
  • 26
  • 132
  • 246
TtheTank
  • 332
  • 3
  • 8
1

Try following code

textField.text is a string value so we are checking it like this

if([txtPhraseBox.text isEqualToString:@""])

{

// There's no text in the box.

}

else

{

NSLog(@"Text Field Text == : %@ ",txtPhraseBox.text);

}
Akshay Aher
  • 2,525
  • 2
  • 18
  • 33
Rohit Wankhede
  • 506
  • 5
  • 15
0

Actually, I ran into slight problems using Raphael's approach with multiple text fields. Here's what I came up with:

if ((usernameTextField.text.length > 0) && (passwordTextField.text.length > 0)) {
    loginButton.enabled = YES;
} else {
    loginButton.enabled = NO;
}
mmackh
  • 3,550
  • 3
  • 35
  • 51
0

Use for text field validation:

-(BOOL)validation{
 if ([emailtextfield.text length] <= 0) {
  [UIAlertView showAlertViewWithTitle:AlertTitle message:AlertWhenemailblank];
  return NO; }  
 return YES;}
Hemant Dixit
  • 1,145
  • 8
  • 12
0

Validation against empty UITextfield. if you don't want that UITextField should not accept blank white spaces. Use this code snippet:

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

    NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
    if  ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound)      {
        return YES;
    }  else  {
        return NO;
    }
}