Check invalid UITextField?

What is the value of a UITextField when it is empty? I seem to be wrong.

I tried (where `phraseBox 'is the name of the mentioned UITextField

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

and

if(phraseBox.text != nil){

What am I missing?

+3
source share
7 answers
// Check to see if it blank
if([phraseBox.text isEqualToString:@""]) {
  // There no text in the box.
}

// Check to see if it NOT blank
if(![phraseBox.text isEqualToString:@""]) {
  // There text in the box.
}
+21
source

found this in apple discussions when looking for the same thing, thought it was bad too. check the string length:

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

}

or optionally trim spaces from it before validation, so the user cannot enter spaces instead. Good for usernames.

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

if([value length] == 0) {
// Alert the user they forgot something
}
+13
source

textField.text - ,

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

{

// There no text in the box.

}

else

{

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

}
+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;
}
+1

In fact, I'm having a little trouble using the Raphael approach with multiple text fields. Here is what I came up with:

if ((usernameTextField.text.length > 0) && (passwordTextField.text.length > 0)) {
    loginButton.enabled = YES;
} else {
    loginButton.enabled = NO;
}
0
source

Using to validate a text field:

-(BOOL)validation{
 if ([emailtextfield.text length] <= 0) {
  [UIAlertView showAlertViewWithTitle:AlertTitle message:AlertWhenemailblank];
  return NO; }  
 return YES;}
0
source

Check for an empty UIText field. unless you want UITextField to not accept empty spaces. Use this piece of code:

- (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;
    }
}
0
source

Source: https://habr.com/ru/post/1749127/


All Articles