1

我使用此代码检查文本字段是否为空。第一次,它会起作用,但是当我更改文本字段值时,警报不起作用。

   -(void)sendclick {
        NSString *msg;
        if(firstnametextfield.text==NULL) {
            msg=@"enter first name";
            NSLog(@"%@",firstnametextfield.text);
        }
        else if(lastnametextfield.text==NULL) {
            msg=@"enter last name"; 
            NSLog(@"%@",lastnametextfield.text);
        }

       else if(emailtextfield.text==NULL) {
             msg=@"enter email address";
            NSLog(@"%@",emailtextfield.text);
        }

       else if(companytextfield.text==NULL) {
            msg=@"enter company name";
            NSLog(@"%@",companytextfield.text);
        }

       else if(phonetextfield.text==NULL) {
            msg=@"enter phone numper"; 
            NSLog(@"%@",phonetextfield.text);
        }
     else {
        msg=@"register success fully";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
     }

    }
4

7 回答 7

3

关于这段代码,有几件事需要考虑:

  1. 正如其他人指出的那样,这不是检查空字符串的正确方法。
    一种。首先,如果您确实要检查未分配的字符串,则应该检查niland not NULL
    湾。其次,字符串也可以分配,但没有字符,所以你应该检查它是否是一个空字符串。
    C。请注意,您通常希望检查这两个条件,并执行相同的操作,因为通常情况下,就业务逻辑而言,nil 字符串和空字符串之间没有区别。
    d。最简单的方法是简单地检查length字符串。这是可行的,因为如果将消息发送到nil对象,它将返回 0,如果它是一个没有字符的实际字符串,它也将返回 0。

    因此,类似于此的检查将起作用:

    if([firstnametextfield.text length] == 0)
    
  2. 您通常想像这样直接检查文本字段的值以进行表单验证。这是因为,在某些情况下(例如,当文本字段位于表格视图单元格中并滚动出屏幕时)文本字段不可用,您将无法访问存储在文本字段中的数据。

相反,您应该在输入文本后立即收集文本,方法是将文本字段的委托设置为视图控制器并实现以下功能:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == firstnametextfield) {
        // Store the first name in a property, or array, or something.
    }
}

然后,当您准备好验证输入的信息时,请使用上面 #1 中的技术检查您存储的值,而不是实际的文本字段值本身。

于 2012-09-04T15:02:56.633 回答
1

您不应将文本字段值与NULL. 而是将其与@"". 您也可以修剪空白字符:

[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
于 2012-09-04T14:24:34.730 回答
1
- (BOOL)isEmptyOrNull:(NSString*)string {
    if (string) {
        if ([string isEqualToString:@""]) {
            return YES;
        }
        return NO;
    }
    return YES;
}


-(void)sendclick {
    NSString *msg;
    if([self isEmptyOrNull:firstnametextfield.text]) {
        msg=@"enter first name";
        NSLog(@"%@",firstnametextfield.text);
    }
    .....
    .....
    .....
于 2012-09-04T14:43:26.017 回答
0

尝试使用

if([lastnameTextField.text isEqualToString:@""])
{
 msg = @"Enter last name";
 NSLog(@"%@",lastnametextfield.text);
}

这是您检查 NSString 是否为空的方式。

于 2012-09-04T14:40:25.740 回答
0

您不能使用 == 或 != 运算符比较字符串对象。所以这里是正确的:

   -(void)sendclick
{
     NSString *msg;
    if([firstnametextfield.text isEqualToString:@""])
    {
        msg=@"enter first name";
        NSLog(@"%@",firstnametextfield.text);
    }
    else
        if([lastnametextfield.text isEqualToString:@""])
    {
        msg=@"enter last name"; 
        NSLog(@"%@",lastnametextfield.text);
    }

   else if([emailtextfield.text isEqualToString:@""])
    {
         msg=@"enter email address";
        NSLog(@"%@",emailtextfield.text);
    }

   else if([companytextfield.text isEqualToString:@""])
    {
        msg=@"enter company name";
        NSLog(@"%@",companytextfield.text);
    }

   else if([phonetextfield.text isEqualToString:@""])
    {
        msg=@"enter phone numper"; 
        NSLog(@"%@",phonetextfield.text);
    }
  else
  {

    msg=@"register success fully";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
 }

}

我希望这对你有帮助。

于 2012-09-04T14:41:02.587 回答
0
   -(void)sendclick 
    {
        if ([self ControlValidation]==YES)
        {

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucess" message:@"Registration done successfully" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert  show];
        }
    }
    -(BOOL)ControlValidation
    {
        if ([self isEmpty:firstnametextfield.text ]==YES)

        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is    empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert  show];
            return NO;
        }
        if ([self isEmpty:lasttnametextfield.text ]==YES)

        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert  show];

            return NO;
        }
        return YES;
    }


    -(BOOL)isEmpty:(NSString *)str

    {
        if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
        {
            return YES;

        }

        return NO;
    }
于 2014-12-24T12:20:09.220 回答
0
  -(void)validateTextFields
        {
            if ([self Validation]==YES)
            {

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucess" message:@"Registration done successfully" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert  show];
        }
    }
    -(BOOL)Validation
    {
        if ([self isEmpty:firstnametextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is    empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];
        return NO;
    }
    if ([self isEmpty:lasttnametextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];

        return NO;
    }
if ([self isEmpty:Usernametextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is    empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];
        return NO;
    }
    if ([self isEmpty:phonetextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];

        return NO;
if ([self isEmpty:emailtextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is    empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];
        return NO;
    }
    if ([self isEmpty:passwordtextfield.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];

        return NO;
    }
    }
    return YES;
}


-(BOOL)isEmpty:(NSString *)str

{
    if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
    {
        return YES;

    }
于 2016-03-23T11:34:48.320 回答