Games
Problems
Go Pro!

Validating E-mail Address Format

Reference > Science > Technology > Beginner Programming Tips
 

You have visitors to your website, and you want them to sign up for a login. You want them to provide you with an e-mail address, and perhaps you are going to use a randomly generated password to help verify that they have entered a valid e-mail address.

However, that might not be enough for you. Why not? Because if you don't have any sort of "tests" in place to help verify that the visitor didn't type an e-mail address incorrectly, they might never receive the confirmation e-mail because they didn't properly format their e-mail address. But they won't realize this, and they'll assume it's your fault they didn't get an e-mail.

So let's see if we can help out the folks who are likely to type their e-mail address incorrectly. The first (and most obvious) solution is to force them to type their e-mail address twice. Then you can check to be sure that the two match. If they don't, you make them retype it. That solves the problem of people who accidentally make typing mistakes.

But you may also want to block people from entering e-mail addresses that are not properly formatted. Common mistakes made by visitors include: forgetting to put the domain name, forgetting to put the extension, and forgetting to put the "personal" part of the e-mail address. In other words, they'll do one of the following:

joe@mydomain
mydomain.com
joe

Yeah, I'm not kidding. I see all three of those with great regularity. Even the last one, believe it or not. Amazing that someone would think "I'll just put my name, and it'll get to me!"

So what do we do? Well, notice that in all of the examples, either the "at sign" (@) or the period (.) are missing. In one case, both are missing. So to prevent people from making these careless mistakes, we can check to make sure that the e-mail address contains both a period and an "at sign".
 

Function IsValidEmail(Email As String) As Boolean
   If InStr(1, Email, ".") = 0 Then
      IsValidEmail = False
      Exit Function
   End If

   If InStr(1, Email, "@") = 0 Then
      IsValidEmail = False
      Exit Function
   End If
   IsValidEmail = True
End Function


Simple, right? You might want to get a little more clever, and block other possibilities. For example, you might think to yourself "Let's make sure the period comes after the 'at sign.'" Not a bad idea. Except... it is a bad idea, depending on how you implement it. If you check to make sure that there is a period after the "at sign," that's good. But don't check to make sure that the "at sign" comes before a period, because that might not work. Take a look at this e-mail address:

joe.shmoe@mydomain.com

See? That e-mail address does have a period after the "at sign," but it also has a period before the "at sign." So let's try this little piece of code, to make sure we get it right:
 

Function IsValidEmail(Email As String) As Boolean
   Dim Amp As Integer
   Dim Per As Integer
   'Contains a period
   If InStr(1, Email, ".") = 0 Then
      IsValidEmail = False
      Exit Function
   End If

   'Contains an "at sign"
   If InStr(1, Email, "@") = 0 Then
      IsValidEmail = False
      Exit Function
   End If

   '"at sign" before last period
   Amp = InStr(1, Email, "@")
   Per = InStrRev(Email, ".")
   If Amp > Per Then
      IsValidEmail = False
      Exit Function
   End If
   IsValidEmail = True
End Function


You see what I've done, right? I used the Reverse InStr (InStrRev) function to find the last period in the e-mail address, and used InStr to find the first "at sign." Then I checked to make sure the first "at sign" was before the last period.

None of this guarantees that the e-mail address entered is actually valid, but it at least saves a lot of people from careless mistakes.

Questions

1.
Write code that considers an e-mail address valid only if it is from an "org" domain.
2.
Write code to only accept e-mail addresses which have only one "at sign."
3.
Write code that won't allow e-mail addresses which begin with or end with a period.
4.
Write code that won't accept an e-mail address less than 7 characters long.
Assign this reference page
Click here to assign this reference page to your students.
Generating Random PasswordsGenerating Random Passwords
Unit IndexUnit Index
 

Ask Professor Puzzler

Do you have a question you would like to ask Professor Puzzler? Click here to ask your question!
Over 3,000 Pages of Free Content
We've been providing free educational games and resources since 2002.

Would you consider a donation of any size to help us continue providing great content for students of all ages?

Like us on Facebook to get updates about new resources
Home
Pro Membership
About
Privacy