user1904273
user1904273

Reputation: 4764

iOS/objective-c: Function to strip characters from phone number

I created the following method to strip characters from a phone number but calling it is triggering error "use of undeclared identifier". Can anyone see what I am doing wrong? Do I have to put a reference to this in .h file? Or why won't it work.

-(id)stripTel:(NSString*) phoneno {
    NSString *condensedPhoneno = [[phoneno componentsSeparatedByCharactersInSet:
                                   [[NSCharacterSet characterSetWithCharactersInString:@"+0123456789"]
                                    invertedSet]]
                                  componentsJoinedByString:@""];
    return condensedPhoneno;
}
-otherFunction {
NSString *oldnum = @"2334332(21)33-)";
NSString *newnum = stripTel:oldnum;
NSLog(@"newnum%@",newnum);
}

Upvotes: 0

Views: 168

Answers (2)

Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 98

Use below code to trim the extra characters from your textfield

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

 {

if(textField == _txtFld_MobileNo ) 

{

  if (range.length == 1) 

{

            // Delete button was hit.. so tell the method to delete the last char.
            _txtFld_MobileNo.text = [self formatPhoneNumber:totalString deleteLastChar:YES];

        }

 else 

{

            _txtFld_MobileNo.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];

  }

        return false;

}


-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar

 {

    if(simpleNumber.length==0) return @"";
    // use regex to remove non-digits(including spaces) so we are left with just the numbers
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\s-\\(\\)]" options:NSRegularExpressionCaseInsensitive error:&error];
    simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];

    // check if the number is to long

    if(simpleNumber.length>10)
 {
        // remove last extra chars.
        simpleNumber = [simpleNumber substringToIndex:10];

    }


    if(deleteLastChar)
 {
        // should we delete the last digit?
        simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1];

    }

    // 123 456 7890
    // format the number.. if it's less then 7 digits.. then use this regex.
    if(simpleNumber.length<7)
        simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d+)"
                                                               withString:@"($1) $2"
                                                                  options:NSRegularExpressionSearch
                                                                    range:NSMakeRange(0, [simpleNumber length])];

    else   // else do this one..
        simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})(\\d+)"
                                                               withString:@"($1) $2-$3"
                                                                  options:NSRegularExpressionSearch
                                                                    range:NSMakeRange(0, [simpleNumber length])];
    return simpleNumber;
}

Upvotes: 0

Andrey
Andrey

Reputation: 2729

You need to call:

NSString *newnum = [self stripTel:oldnum];

and I can see one more mistake, should be:

- (void) otherFunction {

Upvotes: 3

Related Questions