10 examples of 'javascript format phone number while typing' in JavaScript

Every line of 'javascript format phone number while typing' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
267function formatPhone(ph) {
268 var numbers = ph.replace(/\D/g, ''),
269 char = {
270 0: '(',
271 3: ') ',
272 6: ' - '
273 };
274 if (numbers.length !== 10) return false;
275 var ret = '';
276 for (var i = 0; i < numbers.length; i++) {
277 ret += (char[i] || '') + numbers[i];
278 }
279 return ret;
280}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

16function js_phone( phone )
17{
18 if ( parseInt( phone ) == 0 )
19 return '';
20 var len = phone.length;
21 var result = phone;
22 if ( len > 10 )
23 result = result.insert( -10, ' (' ).insert( -7, ') ');
24 if ( len > 4 )
25 result = result.insert( -4, '-' ).insert( -2 , '-' );
26 return ( len > 10 ? '+' : '' ) + result;
27}
1function formatPhoneNumber(phoneNumberString: string) {
2 var cleaned = ("" + phoneNumberString).replace(/\D/g, "");
3 var match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/);
4
5 if (match) {
6 var intlCode = match[1] ? "+1 " : "";
7 return [intlCode, "(", match[2], ") ", match[3], "-", match[4]].join("");
8 }
9
10 return phoneNumberString;
11}
42public getDigits(phone: string): string {
43 const numbers = phone.match(/\d+/g)
44 if (numbers === null) return ''
45 return numbers.join('')
46}
3export function formatPhone(params/*, hash*/) {
4 var phone = params[0];
5
6 if( !phone ){
7 console.warn(
8 "%c{{format-phone}} Invalid phone number passed: %s",
9 "color: orange;", // http://www.w3schools.com/html/html_colornames.asp
10 phone
11 );
12 return phone;
13 }
14
15 return String(phone).replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
16}
23function createPhoneNumber(numbers) {
24 const numbersStr = numbers.join('')
25 const firstPart = numbersStr.substring(0, 3)
26 const secondPart = numbersStr.substring(3, 6)
27 const thirdPart = numbersStr.substring(6, 10)
28
29 return `(${firstPart}) ${secondPart}-${thirdPart}`
30}
155function _phone() {
156 var value = '';
157 for (var i = 0, ln = format.length; i < ln; i += 1) {
158 value += (format.charAt(i) === '#') ? Math.floor(Math.random() * 9) : format.charAt(i);
159 }
160 return value;
161}
3export function formatNumber(phoneNumber: string) {
4 const re = /(\d{3})-?(\d{3})-?(\d{4})/g
5 return phoneNumber.replace(re, '($1) $2-$3')
6}
146function phone(format, cb) {
147 // template: {phone(format)}
148 if (format && cb) {
149 // template: {phone()}
150 } else {
151 cb = format;
152 format = '#### ####';
153 }
154 // replace with faker.PhoneNumber.phoneNumberFormat(format) if Faker.js > 0.1.3 has been published
155 function _phone() {
156 var value = '';
157 for (var i = 0, ln = format.length; i < ln; i += 1) {
158 value += (format.charAt(i) === '#') ? Math.floor(Math.random() * 9) : format.charAt(i);
159 }
160 return value;
161 }
162 cb(_phone());
163}
112formatPhone: function formatPhone(phoneNumber) {
113 return (0, _formatNumber2.default)({
114 phoneNumber: phoneNumber,
115 areaCode: regionSettings.areaCode,
116 countryCode: regionSettings.countryCode
117 });
118},

Related snippets