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 :: Remove specific object from the Array in Javascript 
Javascript :: play sound javascript 
Javascript :: how to get tomorrow date in javascript 
Javascript :: alphabet regex js 
Javascript :: jquery onlcick css change 
Javascript :: react native flatlist get visible items 
Javascript :: shorthand for document.ready 
Javascript :: jquery cdn cloudflare 
Javascript :: giving height full in next image 
Javascript :: javascript get css variable 
Javascript :: await async sleep 
Javascript :: javascript set text of div 
Javascript :: javascript ucwords 
Javascript :: uppercase javascript 
Javascript :: get browser language 
Javascript :: javascript listen for double click 
Javascript :: css in console.log 
Javascript :: ajax header 
Javascript :: discord.js get user by id 
Javascript :: styling scrollview height in react native 
Javascript :: discord.js cooldown 
Javascript :: domain regex 
Javascript :: create element ns svg 
Javascript :: delete first character javascript 
Javascript :: hello world javascript 
Javascript :: angular input press enter 
Javascript :: how to drawImage on center of canvas 
Javascript :: write in utf8 fs 
Javascript :: node js list all installed modules 
Javascript :: js queryselector names 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =