Skip to content

Instantly share code, notes, and snippets.

@yusuphwickama
Last active December 5, 2018 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yusuphwickama/875aafda02eaeb88503de91cd7a0c78f to your computer and use it in GitHub Desktop.
Save yusuphwickama/875aafda02eaeb88503de91cd7a0c78f to your computer and use it in GitHub Desktop.
This is a class that can be used to Validate phone numbers in Tanzania based on a telecomm network, or if it fits the standard phone number format. It can also accept unformated number and return one that fits the format.
/*
* Copyright (c) 2018, Yusuph Wickama (WickerLabs). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS CLASS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.wickerlabs.gasdelivery.util;
public class TZPhoneNumberUtils {
/*
* This can be adapted to any number format of any telecoms networks in Tanzania.
* The example below detects whether the number is a valid TTCL issued number following the standard number format.
* It will return `true` for numbers such as the ones below and may other that fit the standard:
* '+255639123456','+255739123456','255739123456','0639123456','0739123456'
* */
public static boolean isTTCL(String phoneNumber) {
/*
* Here the (63|73) capture group defines the format for TTCL.
* So for TiGO it may look something like (65|67|71)
* */
phoneNumber = phoneNumber.replaceAll("\\s+",""); // Clear spaces
String regex = "^(\\+255|255|0)+(63|73)+[1-9][0-9]{6}";
return phoneNumber.matches(regex);
}
public static boolean isValidTZNumber(String phoneNumber) {
phoneNumber = phoneNumber.replaceAll("\\s+",""); // Clear spaces
return phoneNumber.matches("^(\\+255|255|0)+([67])+[1-9]{2}+[0-9]{6}");
}
public static String getStandardTZNumber(String num) {
String phoneNumber = num.replaceAll("\\s+",""); // Clear spaces
String patternOne = "^(\\+255)+([67])+[1-9]{2}+[0-9]{6}";
String patternTwo = "^(0)+([67])+[1-9]{2}+[0-9]{6}";
String patternThree = "^(255)+([67])+[1-9]{2}+[0-9]{6}";
String patternFour = "^([67])+[1-9]{2}+[0-9]{6}";
if (phoneNumber.matches(patternOne)) {
return phoneNumber;
} else if (phoneNumber.matches(patternTwo)) {
return "+255" + phoneNumber.substring(1);
} else if (phoneNumber.matches(patternThree)) {
return "+" + phoneNumber;
} else if (phoneNumber.matches(patternFour)) {
return "+255" + phoneNumber;
}
return phoneNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment