Skip to content Skip to sidebar Skip to footer

Formatting The Phone Number

The below textbox is printing the value of the phone number after getting it from the database.
Copy

Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

Solution 2:

phone = "0123456789"formated_phone = "("+phone.substring(0,3)+")"+phone.substring(3,6)+"-"+phone.substring(6,11)

Solution 3:

constformatPhoneNumber = (phoneNumber) => {
  // convert the raw number to (xxx) xxx-xxx formatconst x = phoneNumber && phoneNumber.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
  return !x[2] ? x[1] : `(${x[1]}) ${x[2]}${x[3] ? `-${x[3]}` : ''}`;
};

console.log(formatPhoneNumber("1111111111"));

Post a Comment for "Formatting The Phone Number"