Skip to content

Instantly share code, notes, and snippets.

@choyan
Last active October 21, 2021 17:55
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 choyan/d32263a638360eb67ab5bb57096c62ae to your computer and use it in GitHub Desktop.
Save choyan/d32263a638360eb67ab5bb57096c62ae to your computer and use it in GitHub Desktop.
Türk cep telefonu numarası formatı. Turkish Phone Code format. (Javascript)
// Inspired from this answer by maerics https://stackoverflow.com/a/8358141/1648286
function phoneNumberFormatView(phoneNumberString) {
let cleaned = ('' + phoneNumberString).replace(/\D/g, '');
let match = cleaned.match(/^(90|)?(\d{3})(\d{3})(\d{2})(\d{2})$/);
if (match) {
let intlCode = (match[1] ? '+90 ' : '');
return [intlCode, match[2], ' ', match[3], ' ', match[4], ' ', match[5]].join('');
}
return null;
}
export function phoneNumberFormatView(phoneNumberString: string): string {
let cleaned = ('' + phoneNumberString).replace(/\D/g, '');
let match = cleaned.match(/^(90|)?(\d{3})(\d{3})(\d{2})(\d{2})$/);
if (match) {
let intlCode = (match[1] ? '+90 ' : '');
return [intlCode, match[2], ' ', match[3], ' ', match[4], ' ', match[5]].join('');
}
return null;
}
@choyan
Copy link
Author

choyan commented Oct 21, 2021

Output

console.log(phoneNumberFormatView('+905530768278')); // +90 553 076 82 78
console.log(phoneNumberFormatView('5530768278')); // 553 076 82 78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment