验证空的 UITextField

Validating empty UITextField

如果用户在我的应用程序中遗漏了任何 UITextfield,我想提示他们。 我的以下代码不起作用,

-(IBAction)SignUpclicked:(id)sender{
if ([passwordText.text isEqualToString:0] && [usernameText.text isEqualToString:0] && [AddressText.text isEqualToString:0] && [PhoneText.text isEqualToString:0]){

    alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Fill all the Field" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

}

您的回复将是巨大的帮助。

您可以测试字符串长度或将其与空字符串进行比较。

if (text.length == 0) { ... }

if ([text isEqualToString:@""]) { ... }

步骤 1] 时间字符串为空 space.
因为如果用户输入白色 space,我们必须将其删除。

NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
NSString *trimmedString = [passwordText.text stringByTrimmingCharactersInSet:whitespace];


步骤 2] 将结果字符串与其长度或空字符串进行比较

if(trimmedString.length == 0)
{
// String is empty
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Fill all the Field" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];

}
else
{
// String is not empty
}

给你,

- (IBAction)SignUpclicked:(UIButton *)sender {
    if ([[self localtrim:passwordText.text] isEqualToString:@""] || [[self localtrim:usernameText.text] isEqualToString:@""] || [[self localtrim:AddressText.text] isEqualToString:@""] || [[self localtrim:PhoneText.text] isEqualToString:@""]){

        alert =  [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Fill all the Field" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [alert show];
    }
}
-(NSString *)localtrim:(NSString *) text {
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
    NSString *trimString = [text stringByTrimmingCharactersInSet:whitespaceSet];
    return trimString;
}