-1

I am using the following code to display phone number in text field in the following format

and the format is 123-456-7890

and the code is working fine

and the code is as follows

- (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 ([string isEqualToString:@""]) 
{
    return YES;
} 

if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound|| [string rangeOfString:@"-"].location != NSNotFound|| charCount > 12) {
    return NO;
}

if (charCount == 3 || charCount == 7) {
    newString = [newString stringByAppendingString:@"-"];
}

amountField.text = newString;

return NO;


}

I am using the UItextfield delegate method.

But when I am editing the text field up to "-" that mean (if i tried to change the number in text field 123-456-7890 it is not validating that mean 123-456 if i again enter the remaining number from here it is displaying as 123-4567890

can any one please help me how to validate this thing enter image description here

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
user564963
  • 2,284
  • 5
  • 24
  • 33
  • 1
    Do you not care about international phone numbers for this field? They can be more than 12 characters long. – chown Sep 09 '11 at 05:25

4 Answers4

1

You have a good tutorial about validating both US and international phone numbers

http://blog.stevenlevithan.com/archives/validate-phone-number

balexandre
  • 73,608
  • 45
  • 233
  • 342
0

this code use easy to check 111-111-1111 format phone numbers

 NSString *phoneRegEx = @"[0-9]{3}+-[0-9]{3}+-[0-9]{4}";
            NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegEx];
            if([phoneTest evaluateWithObject:phone]!=YES){

           //your code
            }
            else
            {
            //your code

            }
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

Have a look at this code this might help u a bit

txtlpmobile.text is the string(Mobile no ur gonna enter)

 int length = [self getLength:txtLpMobile.text];
            if(length == 10) {
                if(range.length == 0)
                    return NO;
            }
            if(length == 3){
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num];

                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];

                }
            } else if(length == 6) {
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
                }
            }

            NSUInteger newLength;
            newLength = [txtLpMobile.text length] + [string length] - range.length;
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));

for formatting number

-(NSString*)formatNumber:(NSString*)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = [mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
    }
    return mobileNumber;
}

for getting length

-(int)getLength:(NSString*)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = [mobileNumber length];

    return length;
}
mandeep
  • 384
  • 1
  • 4
  • 10
0

This is how I have done it before in the past. This is in no way efficient, but gets the job done, and is for U.S. numbers only.

- (NSString *) getLongPhoneNumber
{   
    NSString *storedNumber = @"15125551212"
    /* ^^ some incoming phone number */

    if(storedNumber == nil){
        storedNumber = @"5125551212";
    }

    //NSLocale *locale = [NSLocale currentLocale];
    //NSString *localeString = [locale localeIdentifier];

    NSString *tempStr = nil;
    NSRange range;
    range.length = 3;
    range.location = 3;
    NSString *areaCode = [storedNumber substringToIndex:3];
    NSString *phone1   = [storedNumber substringWithRange:range];
    NSString *phone2   = [storedNumber substringFromIndex:6];

    tempStr = [NSString stringWithFormat:@"1 (%@) %@-%@", areaCode, phone1, phone2];
    return tempStr;
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186