Reputation: 4901
I use this code to check if text fields are empty or not. The first time, it will work, but when I change the text field values, the alert does not works.
-(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];
}
}
Upvotes: 1
Views: 2188
Reputation: 19
-(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;
}
Upvotes: 0
Reputation: 1741
-(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;
}
Upvotes: 0
Reputation: 25740
There are a couple of things to consider about this code:
As others have pointed out, this is not the correct way to check for an empty string.
a. First, if you do want to check for a string that is not allocated, you should be checking for nil
and not NULL
.
b. Secondly, the string could also be allocated, but have no characters, so you should check to see if it is an empty string as well.
c. Note that you typically want to check BOTH of those conditions, and do the same thing, because, usually, there is no difference between a string that is nil and one that is empty as far as the business logic is concerned.
d. The easiest way to do this is to simply check the length
of the string. This works, because if a message is sent to a nil
object, it will return 0, and if it is an actual string with no characters, it will also return 0.
Therefore, a check similar to this would work instead:
if([firstnametextfield.text length] == 0)
You typically do not want to check the value of a text field directly like this for form validation. This is because, under certain situations (like when a text field is in a table view cell and scrolls off of the screen) the text field is not available and you won't have access to the data stored in the text field.
Instead, you should collect the text immediately after it is entered by setting the delegate of the text field to your view controller and implementing the following function:
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == firstnametextfield) {
// Store the first name in a property, or array, or something.
}
}
Then, when you are ready to validate the entered information, check the values that you have stored instead of the actual text field values themselves, using the technique from #1 above.
Upvotes: 3
Reputation: 39988
- (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);
}
.....
.....
.....
Upvotes: 1
Reputation: 3760
You can't compare string objects using the == or != operators. So here is the correct one:
-(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];
}
}
I hope that helps you.
Upvotes: 0
Reputation: 1143
Try using
if([lastnameTextField.text isEqualToString:@""])
{
msg = @"Enter last name";
NSLog(@"%@",lastnametextfield.text);
}
This is how you check if a NSString is empty or not.
Upvotes: 0
Reputation: 10994
You shouldn't compare the text field value with NULL
. Rather, compare it with @""
. You can also trim whitespace characters too:
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Upvotes: 1