Skip to content

Instantly share code, notes, and snippets.

@rommyarb
Last active June 14, 2019 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rommyarb/805dcf843f4a506908c4056c5a710c98 to your computer and use it in GitHub Desktop.
Save rommyarb/805dcf843f4a506908c4056c5a710c98 to your computer and use it in GitHub Desktop.
Format Phone Number (JS)
var COUNTRY_CODE = "+62";
function formatPhoneNumber(phone) {
var formatted = "";
if (phone.substring(0, 3) !== COUNTRY_CODE) {
if (phone.substring(0, 2) === COUNTRY_CODE.substr(1)) {
formatted = COUNTRY_CODE + phone.slice(2);
} else if (phone.substring(0, 1) === "0") {
formatted = COUNTRY_CODE + phone.slice(1);
} else {
formatted = phone;
}
} else {
formatted = phone;
}
return formatted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment