How to generate a phone number with eight digits and country code 00223 or +223 with java Faker Library?

Viewed 46

I want to use the Faker java library to generate phone numbers with 00223 or +223 followed by eight digits.The current behavior is that the format of the generated phone number does not match the regexes (^(+223|00223)(\d){8}$)|(^\d{8}$) and would like generate for example 0022378953023 or +22378953023 or 78953023.

This the method I invoke to generate a random phone number:

    private final Faker faker = new Faker(Locale.FRANCE);
    private PhoneNumber randomPhoneNumber() {

        return new PhoneNumber("00223"+faker.phoneNumber().phoneNumber().substring(0, 8));
    }

The phone Number class is:

    public class PhoneNumber {

    @NotBlank
    private String phoneNumber;

    private static final Pattern VALIDATION_PATTERN = Pattern.compile("(^(\+223|00223)(\d){8}$)|(^\d{8}$)");

    public PhoneNumber(String phString) {
        Assert.hasText(phString, "PhoneNumber cannot be blank.");
        Assert.isTrue(VALIDATION_PATTERN.asPredicate().test(phString), "PhoneNumber does not have proper format.");
        this.phoneNumber = phString;
    }

    public String asString() {

        return phoneNumber;
    }

}

Thanks very much in advance.

0 Answers
Related