我使用此代码检查文本字段是否为空。第一次,它会工作,但是当我更改文本字段值时,警报不起作用。
-(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];
}
}
答案 0 :(得分:3)
有关此代码需要考虑的几件事情:
正如其他人所指出的,这不是检查空字符串的正确方法
一个。首先,如果您确实要检查未分配的字符串,则应该检查nil
而不是NULL
。
湾其次,字符串也可以分配,但没有字符,所以你应该检查它是否也是一个空字符串。
C。请注意,您通常希望同时检查这些条件,并执行相同的操作,因为通常情况下,就业务逻辑而言,nil字符串与空字符串之间没有区别。
d。最简单的方法是简单地检查字符串的length
。这是有效的,因为如果将消息发送到nil
对象,它将返回0,如果它是一个没有字符的实际字符串,它也将返回0.
因此,类似的检查可以改为:
if([firstnametextfield.text length] == 0)
您通常不想要直接检查文本字段的值以进行表单验证。这是因为,在某些情况下(例如,当文本字段位于表格视图单元格中并滚动离开屏幕时)文本字段不可用,您将无法访问存储在文本字段中的数据。
相反,您应该在输入文本后立即通过将文本字段的委托设置到视图控制器并实现以下功能来收集文本:
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == firstnametextfield) {
// Store the first name in a property, or array, or something.
}
}
然后,当您准备验证输入的信息时,请使用上面#1中的技术检查您存储的值而不是实际的文本字段值。
答案 1 :(得分:1)
您不应将文本字段值与NULL
进行比较。相反,将其与@""
进行比较。您也可以修剪空格字符:
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
答案 2 :(得分: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);
}
.....
.....
.....
答案 3 :(得分:0)
尝试使用
if([lastnameTextField.text isEqualToString:@""])
{
msg = @"Enter last name";
NSLog(@"%@",lastnametextfield.text);
}
这是检查NSString是否为空的方法。
答案 4 :(得分: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];
}
}
我希望能帮助你。
答案 5 :(得分: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;
}
答案 6 :(得分: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;
}