Serheo/SHSPhoneComponent

How to have a prefix and still do formatting

reejosamuel opened this issue · 3 comments

So i will explain the scenario, so we have a better understanding of the question.

I want a prefix like +1 90 which cannot be backspaced by the users and I'm able to get this using

[self.phoneField.formatter setDefaultOutputPattern:@"+1 90 ## ########"];    
    self.phoneField.formatter.showFormatOnlyIfTextExist = NO;
    [self.phoneField setFormattedText:@""];

but at the same time i want to pattern match what comes after the prefix for e.g.
+1 90 10 12345678 then show a us flag
+1 90 20 12345678 then show a gb flag

    [self.phoneField.formatter addOutputPattern:@"+1 90 ## ########"
                                      forRegExp:@"^19010\\d*$"
                                      imagePath:@"SHSPhoneImage.bundle/flag_us"];

    [self.phoneField.formatter addOutputPattern:@"+1 90 ## ########"
                                      forRegExp:@"^19020\\d*$"
                                      imagePath:@"SHSPhoneImage.bundle/flag_gb"];

My problem is I don't want the user to remove the prefix +1 90
Please can you advice how i can do this?.

Dear, @reejosamuel.
Thank you for using library.

Sure, you can do it by:

[self.phoneField.formatter setDefaultOutputPattern:@"+1 90 ## #######"];
self.phoneField.formatter.showFormatOnlyIfTextExist = NO;
[self.phoneField setFormattedText:@""];

[self.phoneField.formatter addOutputPattern:@"+1 90 ## #######" forRegExp:@"^1\\d*$" imagePath:@"SHSPhoneImage.bundle/flag_ru"];
[self.phoneField.formatter addOutputPattern:@"+1 90 ## #######" forRegExp:@"^2\\d*$" imagePath:@"SHSPhoneImage.bundle/flag_us"];

RegExp matches against valuable symbols (## #######).
Also, make sure to provide your images. Now pod version of library provides without image bundle.

Hey thanks for pointing out that REGEX is matched against the hashes. That fixed it. Images I'm using mine, it was just for the example.

Awesome job with the lib.

You Welcome)