Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript function to format phone number

function formatPhoneNumber(phoneNumberString) {
  var cleaned = ('' + phoneNumberString).replace(/D/g, '')
  var match = cleaned.match(/^(d{3})(d{3})(d{4})$/)
  if (match) {
    return '(' + match[1] + ') ' + match[2] + '-' + match[3]
  }
  return null
}
Comment

how to apply phone number formatting to html with javascript

// A function to format text to look like a phone number
function phoneFormat(input){
        // Strip all characters from the input except digits
        input = input.replace(/D/g,'');
        
        // Trim the remaining input to ten characters, to preserve phone number format
        input = input.substring(0,10);

        // Based upon the length of the string, we add formatting as necessary
        var size = input.length;
        if(size == 0){
                input = input;
        }else if(size < 4){
                input = '('+input;
        }else if(size < 7){
                input = '('+input.substring(0,3)+') '+input.substring(3,6);
        }else{
                input = '('+input.substring(0,3)+') '+input.substring(3,6)+' - '+input.substring(6,10);
        }
        return input; 
}
Comment

format phone number javascript

^(([0-9]{3})|[0-9]{3}-)[0-9]{3}-[0-9]{4}$
Comment

PREVIOUS NEXT
Code Example
Javascript :: simple express server 
Javascript :: syntax of reduce in js 
Javascript :: react social login buttons 
Javascript :: right mouse click js 
Javascript :: javascript sets 
Javascript :: how to make javascript function consise 
Javascript :: check file name in url 
Javascript :: how to pass callback function in javascript 
Javascript :: xml vs json 
Javascript :: google scripts get document 
Javascript :: uncaught exception javascript 
Javascript :: breakout to external link in react js 
Javascript :: math floor html 
Javascript :: react usestate 
Javascript :: scroll js 
Javascript :: Different views for Desktop and mobile Angular 
Javascript :: javascript global function 
Javascript :: trigger sweet alert through javascript 
Javascript :: map js 
Javascript :: how to get gmt time in javascript 
Javascript :: [JsonConverter(typeof(StringEnumConverter))] on list of enums 
Javascript :: Expo camera rotation 
Javascript :: import json file into javascript 
Javascript :: middleware uses 
Javascript :: js nepali phone number validation regex 
Javascript :: mongoose cursor eachasync 
Javascript :: reducer function redux 
Javascript :: how to format a javascript date 
Javascript :: regex pattern to validate phone number in jitterbit 
Javascript :: fs readfile encoding 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =