Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:30:54+00:00 2026-06-11T01:30:54+00:00

I use this code to check if text fields are empty or not. The

  • 0

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];
     }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:30:56+00:00Added an answer on June 11, 2026 at 1:30 am

    There are a couple of things to consider about this code:

    1. 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)
      
    2. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to connect to SQL Server 2012, but it does not
I use this code : MethodInvoker TileLoadCompleteMethodInvoker = delegate() { CleanStatusLabelTimer.Enabled = true; MemoryTextBox.Text
i use this code in my app. just found is not correct when compare
I use this code to select password field from the sql server 2012 db,
Ok. I have this little bit of code that is supposed to use text
I use the jQuery Datepicker for two different text fields. One field for start
I use this code , to log $_SERVER['REMOTE_ADDR']; to my small db my issue
I use this code to update data in database table. Can reuse same code
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically
I use this code to process a date string coming in from a json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.