How to put a check on Empty TextField? - iphone

Str is empty i mean i enter nothing in TextFied even then it's going in else case
NSString *Str= textField.text;
NSLog(#"%#",Str);
if([Str isEqualToString:#""])
{
NSLog(#"Hi");
}

what about
if ( [string length] <= 0 )

use this
if(str == NULL)

you can use
if(!textField.text.length)
or
if([txtField.text isEqualToString:#""])

The Below code will check for the empty textfield and even for the whitespaces.
NSString *firstNameWithNoSpaces = [firstNameTxtField.text stringByReplacingOccurrencesOfString:#" " withString:#""];
if (![firstNameWithNoSpaces length] <= 0 ) {
}

you can also use this
if([textfield.text isEqualToString:#""] || textfield.text== nil )

Related

iPhone Check Empty dictionary

if ([dict objectForKey:#"photo"] !=(id)[NSNull null])
{NSLOG(#"dictinary is not empty")}
This is not working for me. to check empty tag
Use count.
For example:
if ([dict count] == 0) {
NSLog("empty");
}
If you want to check for a key then:
if([dict objectForKey:#"photo"]) {
NSLog(#"There's an object in photo");
}
This is working for me. You can handle Null with this code.
[dict isKindOfClass:[NSNull class]];
If the objectForKey:#"photo" is null when not present you can just do: if ([dictionary objectForKey:#"photo"]) {NSLog(#"dictionary is not empty);}
try this code for check is null value or not
NSString *value=[dict objectForKey:#"photo"];
if (value.length != 0 )
{
NSLog("not empty!");
}
else
{
NSLog("empty!");
}
I have taken into the string like :
NSString *strPhoto=[dict objectForKey:#"photo"];
if (strPhoto ==(id)[NSNull null] || [strPhoto isEqualToString:#""] || strPhoto==nil || [strPhoto length]==0) {
}
It is working for me !!!!!!!!!!

Condition for checking NOT <null> value for NSString in Objective C

Code:
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
Output:
NSString *tempPhone = <null>
Now I want to add if condition, for not null; I tried followings:
if (tempEmail != NULL)
if (tempPhone != Nil)
if (tempPhone != nil)
if (![tempPhone compare:#"<null>"])
if (tempPhone != (NSString*)[NSNull null])
I also Checked this Post.
None of them is working for me. Where I am going wrong??
Try the following code
id phoneNo = [personDict objectForKey:kPhoneKey];
if( phoneNo && ![phoneNo isKindOfClass:[NSNull class]] )
{
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
}
else
{
NSLog(#"NSString *tempPhone is null");
}
I think your personDict does not have an object for the key kPhoneKey, so it is returning nil. When you format nil using %#, you get the string "(null)".
id object = [personDict objectForKey:kPhoneKey];
if (object) {
NSString *tempPhone = [NSString stringWithFormat:#"%#", object];
NSLog(#"NSString *tempPhone = %#",tempPhone);
} else {
// object is nil
}
if (tempPhone != nil || [tempPhone isEqualToString:#"(null)"])
Works for me.
It's like this :
if (![tempPhone isKindOfClass:[NSNull class]] && tempPhone != nil)
if ([str_name isKindOfClass:[NSNull class]])
works for me...
I Checked the webResponse (JSON, in my case).
It was returning me the string with value:
<null>
So, The following condition worked for me:
if (![tempPhone isEqualToString:#"<null>"])
Thanks for all your time. Thanks.
Because compare method returns type NSComparisonResult which is defined as
enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
If the string is the same, it will return NSOrderedSame which have a NSInteger value of 0.
Thus, the following line actually means...
if (![tempPhone compare:#"<null>"]) // `tempPhone` is equals to `#"<null>"`
or in a more understandable explanation, if tempPhone value is equal to #"<null>".
You should write it as
if ([tempPhone compare:#"<null>"] != NSOrderedSame)
or
if (![tempPhone isEqualString:#"<null>"])
In case the string is having a null value. Example in NSLog.. Implement this way..
if ([StringName isKindOfClass:[NSNULL Class]]) {
}
else {
}
First we need to check string length.
if (tempPhone.length == 0)
`
{
//Your String is having empty value (E.x, (null))
}
else
{
// You having some values in this string
}`

How to detect UISearchBar is containing blank spaces only

How to detect if UISearchBar contains only blank spaces not any other character or string and replace it with #""?
You can trim the string with a character set containing whitespace using the NSString stringByTrimmingCharactersInSet message (using the whitespaceCharacterSet):
NSString * searchString = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (![searchString length])
// return ... search bar was just whitespace
You can check as
[yourSearchBar.text isEqualToString:#""]
Hope it helps.
if([searchBar.text isEqualToString:#""] && [searchBar.text length] ==0){
// Blank Space in searchbar
else{
// Do Search
}
Use isEqualToString method of NSString
Use stringByTrimmingCharactersInSet to trim the character from NSString.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
Use as below.
NSString* myString = mySearchBar.text
myString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Here's how you detect and replace it: (assuming the UISearchField is called searchBar)
NSString*replacement;
if ([searchBar.text isEqualToString:#" "])
{
replacement = [NSString stringByReplacingOccurancesOfString:#" " withString:#""];
}
searchBar.text = replacement;
Have a look in the Apple Documentation for NSString for more.
Edit:
If you have more than once space, do this:
NSString *s = [someString stringByReplacingOccurrencesOfString:#" "
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, [someString length])
];
searchBar.text = s;
This worked for me: if you are using #"" or length already to control say a button then this version really does detect the whitespace, if a space has been entered...
if([activeField.text isEqualToString:#" "] && [activeField.text length] ==1){
// Blank Space in searchbar
{
// an alert example
}

how to can i display mobile number in 123-456-7890 format in textfield

i need to enter mobile number in a text field.
i need to display mobile number like this format 123-456-7890.
for eg: 1234567890 is my mobile number,while am entering this mobile number in text field,
for first 3 digits i need to place -,after 3 digits again i need to place -.
if i enter 123 then automatically place - in text field,after 456 place ,no need of placing for further 4 digits.
similar to displaying text in currency format.
but while getting text from that text field i need to get mobile number no need of - like 1234567890,not 123-456-7890.
i think my question is quite clear now,let me add comment if is not.
Thank u in advance.
Just to clarify: As a user enters a phone number into a UITextField, you would like it to automatically insert dashes in the proper places.
The answer is in using the UITextFieldDelegate protocol.
1) Set your controller object as a delegate for the UITextField.
2) You'll find the following method in the protocol:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
This method is called every time a character change occurs in the text field.
3) How robust you want your implementation to be is up to you. You could simply do a count of the current characters and insert dashes after 3 and 6 characters. It would be wise to also reject any non-numeric characters.
Here is a sample implementation. We basically take over the field editing manually - Inserting dashes after the appropriate string lengths and making sure the user can only enter numbers:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:#"-"].location != NSNotFound
|| charCount > 12) {
return NO;
}
if (charCount == 3 || charCount == 7) {
newString = [newString stringByAppendingString:#"-"];
}
textField.text = newString;
return NO;
}
Updated Matthew McGoogan's code : This works fine with back space also..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 8) {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if (charCount == 3 || charCount == 7) {
if ([string isEqualToString:#""]){
return YES;
}else{
newString = [newString stringByAppendingString:#"-"];
}
}
if (charCount == 4 || charCount == 8) {
if (![string isEqualToString:#"-"]){
newString = [newString substringToIndex:[newString length]-1];
newString = [newString stringByAppendingString:#"-"];
}
}
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:#"-"].location != NSNotFound
|| charCount > 12) {
return NO;
}
textField.text = newString;
return NO;
}
return YES;}
I used Matthews post above as a base.
This will format as so: (444) 444-4444
It also handles backspaces, unlike the answer above.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == _txtPhone1 || textField == _txtPhone2 || textField == _txtPhone3 || textField == _txtPhone4)
{
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789-() "];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:#")"].location != NSNotFound
|| [string rangeOfString:#"("].location != NSNotFound
|| [string rangeOfString:#"-"].location != NSNotFound
|| charCount > 14) {
return NO;
}
if (![string isEqualToString:#""])
{
if (charCount == 1)
{
newString = [NSString stringWithFormat:#"(%#", newString];
}
else if(charCount == 4)
{
newString = [newString stringByAppendingString:#") "];
}
else if(charCount == 5)
{
newString = [NSString stringWithFormat:#"%#) %#", [newString substringToIndex:4], [newString substringFromIndex:4]];
}
else if(charCount == 6)
{
newString = [NSString stringWithFormat:#"%# %#", [newString substringToIndex:5], [newString substringFromIndex:5]];
}
else if (charCount == 9)
{
newString = [newString stringByAppendingString:#"-"];
}
else if(charCount == 10)
{
newString = [NSString stringWithFormat:#"%#-%#", [newString substringToIndex:9], [newString substringFromIndex:9]];
}
}
textField.text = newString;
return NO;
}
}
Use
NSString* number = [textField.text stringByReplacingOccurrencesOfString: #"-" withString: #""];

How to check the NSString contains the '%' or not?

I want to ask a question about the objective C. Does the NSString * contains some functions to check the NSString * contains some string in the UITextField.text? For example
NSString *checkString = #"abcd%"
if(checkString contains '%') // I want this function
return YES;
else
return NO;
if([checkString rangeOfString:#"%"].location != NSNotFound)
// hooray!
You can use - (NSRange)rangeOfString:(NSString *)aString. The code will look something like:
NSRange range = [UITextField.text rangeOfString:#"!"];
if (range.length > 0){
NSLog(#"String contains '!'");
}
else {
NSLog(#"No '!' found in string");
}
The code from the previous post is not correct
NSRange range = [UITextField.text rangeOfString:#"!"];
if (range.length >= 0){
NSLog(#"String contains '!'");
}
else {
NSLog(#"No '!' found in string");
}
It should be "range.length > 0"