javascript function to format phone number

52

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
}

format phone number javascript -

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

Comments

Submit
0 Comments