|
|
Beginner Programming Tips and Tricks
Each page contains a helpful programming tip and exercises which encourage
beginners to use what they've learned in a different situation.
|
Properly Formatted Email Addresses
You have visitors to your website, and you want them to sign up for a login. You want them to provide you with an
email address, and perhaps you are going to use a randomly generated password to help verify
that they have entered a valid email 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 email address incorrectly, they might never receive the confirmation email because they didn't properly
format their email address. But they won't realize this, and they'll assume it's your fault they didn't get an email.
So let's see if we can help out the folks who are likely to type their email address incorrectly. The first (and most obvious)
solution is to force them to type their email 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 email 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
email 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 email 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 email address:
joe.shmoe@mydomain.com
See? That email 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
email 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 email address entered is actually valid, but it at least saves a lot
of people from careless mistakes.
Other Scenarios
You could get really sophisticated in the way you validate email addresses; how would you do the following?
- Consider an email address valid only if it is from an "org" domain
- Only accept email addresses which have only one "at sign"
- Don't allow email addresses which begin with or end with a period
- don't accept an email address less than 7 characters long
"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.
Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using
this site in a classroom situation, please visit the Teachers page.
More programming information and other tips can be found at Virtu Software's Ask Doug site.
Want To Try Something Completely Different?
Site Features
| |
|
Search For More Educational Resources
Find more educational, problem solving, and puzzle resources using the Google safe-search below.
|
|
|