Tip

Phone Number Validation

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.