Format Phone Number


/ Published in: Visual Basic
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Function formatPhoneNbr(PhoneNbrStr As String) As String
  2.  
  3. Dim tmpNbr As String
  4. Dim tmpPos As String
  5. Dim tmpLen As Byte
  6. Dim i As Byte
  7.  
  8. ' Get all numbers
  9. For i = 1 To Len(PhoneNbrStr)
  10. tmpPos = Mid(PhoneNbrStr, i, 1)
  11. If IsNumeric(tmpPos) Then
  12. tmpNbr = tmpNbr & tmpPos
  13. End If
  14. Next i
  15.  
  16. ' Get phone number length
  17. tmpLen = Len(tmpNbr)
  18.  
  19. ' Output formatted phone number
  20. Select Case tmpLen
  21. Case 7 ' No area code
  22. formatPhoneNbr = Left(tmpNbr, 3) & "-" & Right(tmpNbr, 4)
  23.  
  24. Case 10 ' Include area code
  25. formatPhoneNbr = "(" & Left(tmpNbr, 3) & ") " & _
  26. Mid(tmpNbr, 4, 3) & "-" & Right(tmpNbr, 4)
  27.  
  28. Case Else
  29. formatPhoneNbr = "Invalid Phone Number"
  30.  
  31. End Select
  32.  
  33. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.