Format Phone Number: Difference between revisions

From Starfish ETL
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


<source lang="vb">
<source lang="vb">
Function FormatPhone(ph)
Function ScriptedField
    dim oldphone
dim phone
dim phone
phone=ph
dim xPos
dim plusPos
dim extension
    oldphone="@@ORG:phone_office@@"
phone=oldphone


phone = Replace(phone,"(","")
if len(phone) <> 0 then
phone = Replace(phone,")","")
'Remove all non-numberic characters except the x and + sign.
phone = Replace(phone," ","")
With CreateObject("vbscript.regexp")
phone = Replace(phone,".","")
.Pattern = "[^0-9x+]"
phone = Replace(phone,"-","")
.Global = True
phone = lcase(phone)
phone = .Replace(phone, "")
if Len(phone)=11 and Left(phone,1)="1" then phone=Mid(phone,2)
End With


if Len(phone)=10 then
plusPos = inStr(1,phone,"+")
FormatPhone = "(" & mid(phone,1,3) & ")" & mid(phone,4,3) & "-" & mid(phone,7)
 
elseif Len(phone)>10 then
if plusPos > 0 then
if mid(phone,11,1)="x" then
'Ignore foreign phone numbers.
FormatPhone = "(" & mid(phone,1,3) & ")" & mid(phone,4,3) & "-" & mid(phone,7)
ScriptedField = oldphone
else
else
FormatPhone = ph
'break off the extension.
end if
xPos = inStr(1,LCase(phone),"x")
else
if xPos > 0 then
FormatPhone = ph
extension = Trim(Right(LCase(phone), (Len(phone) - (xPos) + 1)))
end if
phone = Trim(Left(phone, (xPos - 1)))
End Function
Else
</source>
extension = ""
 
End If
 
'format non-foreign phone numbers and phone numbers without an extension.
 
if Len(phone)=11 and Left(phone,1)="1" then  
<source lang="vb">
phone = Mid(phone,2)
Function FormatPhone(phone)
ScriptedField = mid(phone,1,3) & "-" & mid(phone,4,3) & "-" & mid(phone,7) & extension
dim wPhone
elseif Len(phone)=10 then
dim wPos
ScriptedField = mid(phone,1,3) & "-" & mid(phone,4,3) & "-" & mid(phone,7) & extension
dim wPhoneNbr
dim wPhoneExt
wPhone = phone
wPos = InStr(1, wPhone, "+")
if wPos > 0 then
FormatPhone = phone
else
wPos = InStr(1, LCase(wPhone), "x")
if wPos > 0 then
wPhoneNbr = Trim(Left(wPhone, (wPos - 1)))
wPhoneExt = Trim(Right(LCase(wPhone), (Len(wPhone) - (wPos) + 1)))
else
wPhoneNbr = Trim(wPhone)
wPhoneExt = ""
end if
if IsNumeric(wPhoneNbr) then
if Len(wPhoneNbr) = 7 then
FormatPhone = Left(wPhoneNbr, 3) & "-" & Right(wPhoneNbr, 4) & " " & wPhoneExt
else
else
if Len(wPhoneNbr) = 10 then
ScriptedField = oldphone
FormatPhone = Left(wPhoneNbr, 3) & "-" & Mid(wPhoneNbr, 4, 3) & "-" & Right(wPhoneNbr, 4) & " " & wPhoneExt
end if
else
if Len(wPhoneNbr) = 11 then
FormatPhone = Left(wPhoneNbr, 1) & "-" & Mid(wPhoneNbr, 2, 3) & "-" & Mid(wPhoneNbr, 5, 3) & "-" & Right(wPhoneNbr, 4) & " " & wPhoneExt
else
FormatPhone = phone
end if
end if
end if    
else
FormatPhone = phone
end if
end if
end if
    end if
End Function
End Function
</source>
</source>

Latest revision as of 22:14, 12 October 2015

Here are a couple of sample functions to help you clean up phone numbers.

Function ScriptedField
    dim oldphone
	dim phone
	dim xPos
	dim plusPos
	dim extension
    oldphone="@@ORG:phone_office@@"
	phone=oldphone

	if len(phone) <> 0 then
		'Remove all non-numberic characters except the x and + sign.
		With CreateObject("vbscript.regexp")
			.Pattern = "[^0-9x+]"
			.Global = True
			phone = .Replace(phone, "")
		End With

		plusPos = inStr(1,phone,"+")
	   
		if plusPos > 0 then
			'Ignore foreign phone numbers.
			ScriptedField = oldphone
		else
			'break off the extension.
			xPos = inStr(1,LCase(phone),"x")
			if xPos > 0 then
				extension = Trim(Right(LCase(phone), (Len(phone) - (xPos) + 1)))
				phone = Trim(Left(phone, (xPos - 1)))
			Else
				extension = ""
			End If
			'format non-foreign phone numbers and phone numbers without an extension.
			if Len(phone)=11 and Left(phone,1)="1" then 
				phone = Mid(phone,2)
				ScriptedField = mid(phone,1,3) & "-" & mid(phone,4,3) & "-" & mid(phone,7) & extension
			elseif Len(phone)=10 then
				ScriptedField = mid(phone,1,3) & "-" & mid(phone,4,3) & "-" & mid(phone,7) & extension
			else
				ScriptedField = oldphone
			end if
		end if
    end if
End Function