I'm getting the phone number from the user and I enter it to the DB but everytime it's in a different format:
111-222-3333
(111) 222-3333
111.222.3333
I'd like to have in one format only which is:
(111) 222-3333
How do I do that?
dthurman1432
06-26-2006, 12:43 PM
Public Function PhoneFormat(ByVal strPhoneNumber As String) As _
String
Dim strResult As String
Dim iLength As Integer
Dim strExtraChar As String
Dim strOriginal As String
Dim iSpaceResult As Integer
Dim i As Integer
strOriginal = strPhoneNumber
' Remove any style characters from the user input
strPhoneNumber = Replace(strPhoneNumber, ")", "")
strPhoneNumber = Replace(strPhoneNumber, "(", "")
strPhoneNumber = Replace(strPhoneNumber, "-", "")
strPhoneNumber = Replace(strPhoneNumber, ".", "")
strPhoneNumber = Replace(strPhoneNumber, Space(1), "")
iLength = Len(strPhoneNumber)
'convert any letters to numbers
For i = 1 To iLength
Mid$(strPhoneNumber, i, i) = _
PhoneLetterToDigit(Mid$(strPhoneNumber, i, i))
Next i
' now, if any other chars besides numbers exist, return original string to user
For i = 1 To iLength
Select Case Asc(Mid$(strPhoneNumber, i, i))
Case Is < 48, Is > 57
strResult = strOriginal
End Select
Next i
Select Case iLength
' user entered a lot of numbers;only format the first 10
Case Is > 11
If Left$(strPhoneNumber, 1) = "1" Then
strExtraChar = Mid$(strPhoneNumber, 12)
strPhoneNumber = Mid$(strPhoneNumber, 2, 10)
Else
strExtraChar = Mid$(strPhoneNumber, 11)
strPhoneNumber = Mid$(strPhoneNumber, 1, 10)
End If
' if user included the number 1 before the area code.
'We drop this number
Case Is = 11
If Left$(strPhoneNumber, 1) = "1" Then
strPhoneNumber = Mid$(strPhoneNumber, 2)
Else
' check for a space character
iSpaceResult = InStrRev(strOriginal, Space(1))
If iSpaceResult = 0 Then
' we have no idea what they entered
strResult = strOriginal
GoTo Exit_Proc
Else
strExtraChar = Mid$(strPhoneNumber, iSpaceResult)
strPhoneNumber = Mid$(strPhoneNumber, 1, _
iSpaceResult - 1)
End If
End If
Case Is = 10 ' area code and phone
strPhoneNumber = strPhoneNumber
' user did not include an area code; add 3 spaces
Case Is = 7
strPhoneNumber = Space(3) & strPhoneNumber
' unable to figure out what the user typed
' must be an extentsion and not a 'real' phone number
Case Else
strResult = strOriginal
GoTo Exit_Proc
End Select
'Add sytle characters into phone number (format)
strResult = Format(strPhoneNumber, "\(@@@\)\ @@@\-@@@@") & _
Space(1) & strExtraChar
Exit_Proc:
PhoneFormat = strResult
End Function
Function PhoneLetterToDigit(ByVal strPhoneLetter As String) As _
String
Dim intDigit As Integer
intDigit = Asc(UCase$(strPhoneLetter))
If intDigit >= 65 And intDigit <= 90 Then
If intDigit = 81 Or 90 Then ' Q or Z
intDigit = intDigit - 1
End If
intDigit = (((intDigit - 65) \ 3) + 2)
PhoneLetterToDigit = intDigit
Else
PhoneLetterToDigit = strPhoneLetter
End If
End Function
buntine
06-26-2006, 07:59 PM
If you asked me, I would do the following:
1) Strip any non-numeric characters from the string (using a Regular Expression);
2) Ensure the remaining string is ten characters long;
3) Format the string (place brackets around the first three characters and stick a dash between the sixth and seventh character.
Let me know if you need any further explanation or code examples.
Cheers.
Code sample could be nice buntine.
No offense but I don't think I'll use 113 lines for something like that.
Komarzec
07-19-2006, 01:49 PM
Similar issue here, but I am working with a background process which is taking in a String value that represents a phone number - with only the numeric representation of the phone (i.e. '9205551234').
I need to format the phone based on the location. If US - I need to reformat the string and store in DB using the format (###) ###-####. If Mexico - I need to reformat the string and store using format like ##-####-####... and I need to be able to provide the foundation for formats of other countries in the future. So I want to use a mask implementation.
I really like the way the swing class MaskFormatter handles this, but being this is a background process, I was told to avoid using swing classes.
Is there another java class that provides mask formatting for a String value?
Thanks for your input!
felgall
07-19-2006, 03:52 PM
There are several hundred different phone number formats in use around the world. You can't even guarantee that phone numbers will always be the same length. If you are going to try to standardise on a format then I suggest that you just strip out everything except the numbers the extension indicator (if any) and the + from the front of international numbers that indicates that you need to add the international calling code for your country to the front of the number. eg.
+61298765432x999