Serheo/SHSPhoneComponent

Multiple formats

CavalcanteLeo opened this issue · 1 comments

my code has to work in both formats like this:

"(##) ####-####" OR "(##) #####-####"

it's a telephone number in brazil
(2 digits) + 8 or 9 digits

And I don't have a regex for them,

Anyone could help me?

Actually, i'm going to use with all countries around the world,

Here is my json (i only have coded the brazilian numbers yet)

[{
	"code": "AF",
	"dial_code": "+93",
	"flag": "Afghanistan.png",
        "phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Afghanistan"
}, {
	"code": "AO",
	"dial_code": "+244",
	"flag": "Angola.png",
	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Angola"
},{
	"code": "AR",
	"dial_code": "+54",
	"flag": "Argentina.png",
	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Argentina"
},  {
	"code": "AW",
	"dial_code": "+297",
	"flag": "Aruba.png",

	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Aruba"
}, {
	"code": "AU",
	"dial_code": "+61",
	"flag": "Australia.png",

	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Australia"
}, {
	"code": "AT",
	"dial_code": "+43",
	"flag": "Austria.png",

	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Austria"
},  {
	"code": "BR",
	"dial_code": "+55",
	"flag": "Brazil.png",
	"phoneMask": ["(##) ####-####", "(##) #####-####"],
	"name": "Brazil"
}

.........

I did it by a simply count of the hashes

-(void)prepareTextFields{
     [self.currentCountry.phoneMasks enumerateObjectsUsingBlock:^(NSString * _Nonnull pattern, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0){
            [self.textFieldPhone.formatter setDefaultOutputPattern:pattern];
        }
        
        NSString *regex = [self regexString:pattern];
        [self.textFieldPhone.formatter addOutputPattern:pattern forRegExp:regex];
    }];
}



-(NSString *)regexString:(NSString *)patterString{
    NSUInteger numberOfHashes = [[patterString componentsSeparatedByString:@"#"] count] - 1;

    return [NSString stringWithFormat:@"^\\d{%zd}", numberOfHashes];
}