<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> <!-- data-ad-client=ca-pub-6672515474304268 --> <!-- --><style type="text/css">@import url(https://www.blogger.com/static/v1/v-css/navbar/3334278262-classic.css); div.b-mobile {display:none;} </style> </head><body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar/4865038256408662682?origin\x3dhttp://imacomputernerd.blogspot.com', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
0 comments | Monday, April 23, 2007

I'll be the first to admit that in the beginning I cut a few corners. Putting more focus into making it work quickly than correctly. I think all programmers have done it from time to time, especially on tight time schedules. One mistake that I and MANY others have made in the past is for validation. Checking to see if data even exsists is not enough anymore especially when your dealing with formatted information like email addresses and phone numbers. There are several different ways to validate an email address. From checking to make sure it "@" in it, all the way to querying the web server to verify the actual address entered. In my experience I've found that no matter what you are going to get a few bogus emails, but finding the fine line between application speed and getting the most valid data possible is up to you.



Checking to see if an address includes a "@" and has a extension following a "." can be done very quickly. I generally tend to simply check syntax because I figure if the user doesn't want to give you his/her address than he/she is not really going to want to read your newsletter either. For more complex applications where a actual address is mandatory, I would recommend using a email verify system where you send an email to the applicant with a link to verify his/her email address. That way your gauranteed that he/she entered an actual valid address that he/she has access to. But for simple mailing list or newletter signups I stick with a pain free fast and easy verification to check for a few things.



1. Make sure it has only 1 "@"
2. Make sure it has at least 1 "."
3. Make sure there are no more than 3 charactors after the last "."
4. Make sure it has no "_" after the "@"
5. A brief check to make sure invalid charactors were not used in the address.



Here is what the function to check an address would look like;





Public function chkEmail(theAddress as String)
' returns 1 for invalid addresses
dim atCnt
chkEmail = 0

' chk length
if len(cstr(theAddress)) < 5 then
' a@b.c should be the shortest an
' address could be
chkEmail = 1

' chk format
' has at least one "@"
elseif instr(theAddress,"@") = 0 then
chkEmail = 1

' has at least one "."
elseif instr(theAddress,".") = 0 then
chkEmail = 1

' has no more than 3 chars after last "."
elseif len(theAddress) - instrrev(theAddress,".") > 3 then
chkEmail = 1

' has no "_" after the "@"
elseif instr(theAddress,"_") <> 0 and _
instrrev(theAddress,"_") > instrrev(theAddress,"@") then
chkEmail = 1

else
' has only one "@"
Dim i as integer
atCnt = 0
for i = 1 to len(theAddress)
if mid(theAddress,i,1) = "@" then
atCnt = atCnt + 1
end if
next

if atCnt > 1 then
chkEmail = 1
end if

' chk each char for validity

for i = 1 to len(theAddress)
if not isnumeric(mid(theAddress,i,1)) and _
(lcase(mid(theAddress,i,1)) < "a" or _
lcase(mid(theAddress,i,1)) > "z") and _
mid(theAddress,i,1) <> "_" and _
mid(theAddress,i,1) <> "." and _
mid(theAddress,i,1) <> "@" and _
mid(theAddress,i,1) <> "-" then
chkEmail = 1
end if
next
end if
end function





In this we are passing the email address to the function. Then its checking the address for the above listed items and returning a value. The value states wether or not the email was valid. In this case "1" being invalid, and "0" being valid. With this in mind a simple 'If' statment can control what we do in this scenario. Check it out.




Dim strEmail as string = Request.Form("txtEmail")

If chkEmail(strEmail) = 1 Then
'The address was NOT vaild. Do not proceed, return error message

Else
'The address was valid. Proceed with writing the address
'into the database or whatever action you want to do.

End If





Thats pretty wasy, we said 'If' the result of the Function chkEmail = 1 then we know its bad. Else would mean it passed and we can proceed to capture the good email address.



Again, there are a TON of different ways to verify email addresses. I have found for light use this code to work very well.

Labels: , , , ,

0 Comments:

Post a Comment

<< Home