Created
March 9, 2018 08:38
-
-
Save trhura/dd3d5acf399744c329d96a017c227487 to your computer and use it in GitHub Desktop.
Myanmar Phone Number validation & normalization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Myanmar Phone Number validation & normalization for ES6 | |
// Based on https://github.com/trhura/mm_phonenumber | |
const mobileCode = "(0?9)"; | |
const countryCode = "(\\+?95)"; | |
const ooredooNumber = "(?:9(?:7|6)\\d{7})"; | |
const telenorNumber = "(?:7(?:9|8|7|6)\\d{7})"; | |
const mptNumber = | |
"(?:5\\d{6}|4\\d{7,8}|2\\d{6,8}|3\\d{7,8}|6\\d{6}|8\\d{6}|7\\d{7}|9(?:0|1|9)\\d{5,6})"; | |
const allOperators = `(${ooredooNumber}|${telenorNumber}|${mptNumber})$`; | |
const mmNumber = `^${countryCode}?${mobileCode}?${allOperators}`; | |
export class MMPhoneNumber { | |
static isValid(number) { | |
let phone = number.toString(); | |
return phone.match(mmNumber) != null; | |
} | |
static normalize(number) { | |
let phone = number.toString(); | |
let matches = phone.match(mmNumber); | |
if (matches == null) { | |
throw new TypeError(`${number} is not a valid phone number.`); | |
} | |
return parseInt("959" + matches[3]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment