email validation msgbox YES/NO

albertc30

Well-known Member
Joined
May 7, 2012
Messages
1,091
Office Version
  1. 2019
Platform
  1. Windows
Hi all.

I want to validate a textbox to confirm email has been entered correctly.

I remember this being an issue in the past and it seems it still is.

I have been using the code bellow and was feeling that, I don't want the OK message if the email is okay but rather want a user message asking if the entered email (display email on message) is correct? YES (Continue) NO (returns to email field.)

Code:
[COLOR=#333333]Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)[/COLOR]
[COLOR=#333333]With CreateObject("vbscript.regexp")[/COLOR]
[COLOR=#333333]'.Pattern = "^[\w-\.]+@([\w-]+\.)+[A-Za-z]{2,4}$"[/COLOR]
[COLOR=#333333].Pattern = "^[\w-\.]+@[\w-]+\.+[A-Za-z]{2,4}$"[/COLOR]
[COLOR=#333333]If .test(TextBox1.Value) Then[/COLOR]
[COLOR=#333333]MsgBox "OK"[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]MsgBox "No good"[/COLOR]
[COLOR=#333333]Cancel = True[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

Again, much appreciated for all your time and help.

Cheers
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi

Code:
' DP1 is a text box
Private Sub DP1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim s$
With CreateObject("vbscript.regexp")
    .Pattern = _
    "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
    If .Test(DP1) Then
        s = "OK"
    Else
        s = "no good"
    End If
End With
Select Case MsgBox("I think this email is " & s & _
", would you like to continue?", vbYesNo, DP1)
    Case Is = vbYes
    Case Is = vbNo
        Cancel = True
End Select
End Sub
 
Upvote 0
Hi all, Brazil.

Your code works great.

Having said this, I was wondering, rather than placing any validation at all, after exiting the textbox, user be presented with a msg asking something like "Is "show entered email here" correct?

Then user can check the entered email and click yes or no.

Much appreciated for your time my amigo.

Viva o Brazil!

Cheers.
 
Upvote 0
Hi Albert

Are you in Virginia or East Anglia?

Code:
Private Sub DP1_Exit(ByVal Cancel As MSForms.ReturnBoolean)


If MsgBox("Is this email correct?", 4, DP1) = 7 Then Cancel = 1


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top