Everybody likes to input their phone numbers in a different way, right? This will take any format and convert it immediately to a (123) 456-7890 format. I would recommend that you also add the line source.gototop on your querysave event to ensure the script is processed before the file is saved.
Sub Exiting(Source As Field)
Dim ws As New notesuiworkspace
Dim doc As notesuidocument
Set doc = ws.currentdocument
Dim phone As String, message As String
Dim num As Double
Dim fieldname As String
fieldname = "emp_phone" 'Place the name of the field here
phone = doc.fieldgettext(fieldname)
On Error Goto clear_field
phone = Trim$(phone)
If phone = "" Then
Call doc.fieldsettext(fieldname,"")
Exit Sub
End If
If Cstr(phone) Like "[(]???[)][ ]???[-]????" Then
phone = Mid$(phone,2,3) & Mid$(phone,7,3) & Right(phone,4)
Goto correct
End If
If Cstr(phone) Like "???[-]???[-]????" Then
phone = Left$(phone,3) & Mid$(phone,5,3) & Right(phone,4)
Goto correct
End If
If Len(phone) = 10 Then
Goto correct
Else
message = "You must enter the phone number as 123-456-7890 or 1234567890."
Messagebox message, 16, "Incorrect Entry"
Call doc.GotoField(fieldname )
Exit Sub
End If
correct:
num = Cdbl(phone)
phone = "(" & Left$(phone,3) & ") " & Mid$(phone,4,3) & "-" & Right(phone,4)
Call doc.fieldsettext(fieldname,phone)
Exit Sub
clear_field:
message = "You must enter the phone number as 123-456-7890 or 1234567890."
Messagebox message, 16, "Incorrect Entry"
Call doc.gotofield(fieldname)
Exit Sub
End Sub
This was first published in November 2000