Skip to content

Instantly share code, notes, and snippets.

@giautm
Created September 20, 2018 11:15
Show Gist options
  • Save giautm/b7d333c2ca8c6c05105179c126162a93 to your computer and use it in GitHub Desktop.
Save giautm/b7d333c2ca8c6c05105179c126162a93 to your computer and use it in GitHub Desktop.
VN phone carriers detector
const MOBILE_REGEXP = /^(\+84|0)(3[2-9]|5[2689]|7[06-9]|8[1-5])[0-9]{7}$/
const PREFIX_CARRIERS = {
viettel: ["32","33","34","35","36","37","38","39"],
mobifone: ["70","79","77","76","78"],
vinaphone: ["83","84","85","81","82"],
vietnamobile: ["52","56","58"],
gmobile: ["59"]
};
function phoneCarrier(phone) {
const matches = (phone || '').match(MOBILE_REGEXP)
if (matches !== null) {
return Object.keys(PREFIX_CARRIERS)
.find(prefix => PREFIX_CARRIERS[prefix].includes(matches[2]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment