Hi,
I've got a text box in an Access Db that the user must enter a valid email address into. (This is done by copying from Outlook 2010). As a result, sometimes copying from Outlook brings the contact's name and angular brackets. Like so: Fred Bloggs <fred@bloggs.co.uk[/EMAIL"]fred@bloggs.co.uk"]fred@bloggs.co.uk[/EMAIL]>. Sometimes, however, if the contact is just an email address in Outlook, that's all that gets copied. A regular, vanilla email address.
So there's two things I'm trying to do:
If what's been pasted in from Outlook includes a name and angular brackets, remove 'em.
If what's been pasted in isn't a valid email address, complain. What's entered might be nothing, or it might be "bob@bob." (lacks TLD)
I was hoping to use a regex
Code:
.pattern = "[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}"
to identify the email address
but I don't really much idea how to go about writing such a relatively complicated routine for my level of experience.
I've been advised that if the email were always enclosed in the final <> I could:
Code:
Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
email = Left$(email, Len(email) - 1)
End If
fmt = email
End Function
or
Code:
replace(mid(email,instrrev(email,"<")+1,len(email)),">","")
But this isn't the case.
I was also told that for a regexp check I should add a reference to the "Microsoft VBScript Regular Expressions library" (tools > references). I have done this.
Code:
Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
email = Left$(email, Len(email) - 1)
End If
fmt = email
With New RegExp
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "^\S+@\S+\.\S+$"
If Not .Test(fmt) Then fmt = ""
End With
End Function
This apparently returns a valid email address, or "" if its not valid.
I might need my hand holding a bit if anyone has the inclination!