Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

how to check that UITextField is empty or not?????????

Hello,

suppose i have two control one is textField and second is button for submitting to value of textField.

I can click on button then and then only i have entered values.

If i press button without put cursor on textField then it should give
me alert when textField has not any value.


I have tried like,

NSString *string = textField.text;
if(string isEqualToString:@"")
{
code of alert;
NSLog(@"textField is empty");
}

But its not working for me.

Then
if([string lenght]>0)
{
processing code
}
esle{
code for alert and ;;;;;
}

Its also not working for me.

Please anyone tell me where am i wrong????????????

Thanks.

Message was edited by: vavdiyaharesh

mac book, Mac OS X (10.5.4)

Posted on Aug 29, 2008 12:34 AM

Reply
Question marked as Best reply

Posted on Aug 29, 2008 2:01 AM

[textField.text length] > 0 works fine here
5 replies

Aug 29, 2008 6:05 AM in response to Ostrowsky S

It's possible that someone put a space in your textfield and length won't return 0. You should perform a trim on it like:

NSString *value = [textField.text stringByTrimmingCharactersInSet:[[NSCharacterSet whitespaceCharacterSet]]];

This will provide with a nicely trimmed string and you can then test against length:

if([[value length]] == 0) {
// Alert the user they forgot something
}

Message was edited by: timothyBraun

Oct 2, 2008 1:17 PM in response to timothyBraun

Thanks for the suggestion of trimming the string first! I had a similar problem but just checking the length of the text property wasn't working! Even if I clear out all the characters in the UITextfield, for some reason it still thinks there is 1 character there (a space i assume)...so trimming it before checking the length works great.

how to check that UITextField is empty or not?????????

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.