Basic Email Validation

OK, this is not by any means a full proof way to validate an email address.  This is more of a way to dummy-proof your online forms so that users have to enter an email address in a more consistent format.  In order to truthfully validate an email address, you need to have a third party component installed on your server that will verify each email address submitted to your website.  In our experience though, most small businesses and individuals do not get into this because of a lack of know-how and/or lack of an IT budget.  Besides that, most third party hosting companies will not install a component like this on their server for you to use because it slows downs their server performance.  So what can you do to verify an email address.  Well, one quick and dirty way to do it is to verify that the email address contains an “@” symbol and a “.” symbol.  If you don’t include some form of basic email validation like this on your email address form fields, you will be shocked by how many users will enter their name in your email field instead of their email address.  This script at least stops that from happening.

To do this, create a form page called form.asp and copy the below code into your page:

<form name="YourFormNameHere" method="Post" action="confirm.asp">
Email: <input type="text" name="Email" size="50">
<input type="submit" name="Submit" value="Submit">

In this case, we will make the Email field required.  Next, create a form confirmation web page called confirm.asp and copy the below code into your page:

DIM strEmail
strEmail = Request.Form("Email")
IF strEmail <> "" AND inStr(strEmail,"@") <> 0 AND inStr(strEmail,".") <> 0 THEN
' Enter form to database or send via email

' An invalid email has been entered
Response.Write "<p>Please click Back on your browser and update the following field:</p>"

     IF strEmail <> "" THEN
     ' The email field is blank
     Response.Write "<b>• Email</b><br>"
     END IF

     IF inStr(strEmail,"@") <> 0 THEN
     ' The email entered does not include the @ symbol
     Response.Write "<b>• You entered an invalid Email Address</b><br>"
     END IF

     IF inStr(strEmail,".") <> 0 THEN
     ' The email entered does not include a (.)
     Response.Write "<b>• You entered an invalid Email Address</b><br>"
     END IF


This script verifies that the email field is not blank and that the email address also contains a “@” and a “.” or it displays a “Click Back on your Browser…” message.  You can obviously take this much further if you wanted to depending on your situation.  If you only wanted to except emails that ended in “.com” or “.de” you could enter “.com” in place of the “@” symbol and change the “<> 0” to “= 0”.  So even though this script will not actually verify that the email address entered is totally valid, it at least helps reduce the amount of potential fake email addresses you could have to deal with.

Happy validating!

< Back to ASP Scripts