Normal Topic verify format of e-mail address (Read 1613 times)
addison1
Member
*
Offline


No personal text

Posts: 43
Joined: Aug 1st, 2004
verify format of e-mail address
Jun 7th, 2005 at 2:08pm
Print Post Print Post  
I need to input customer's e-mail addresses and I'd like to verify that what goes in at least looks like an e-mail address.
I'm using @inStr to do things like verify that there an "@" and a "." in the address string, but it's far from fool proof. Any ideas?
Thanks
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: verify format of e-mail address
Reply #1 - Jun 8th, 2005 at 2:36am
Print Post Print Post  
I have used some javascript and Visual Basic and regular expressions to check for valid email addresses.  It is going to be much more complex than you think, but there are some samples out there already. 

Here is a JAVASCRIPT code:
Code
Select All
// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
	if (db) alert('email address contains invalid characters');
	return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
	if (db) alert("email address contains non ascii characters.");
	return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
} 

Source is http://javascript.about.com/od/completescripts/a/email.htm

============================

Here is a Visual Basic version:
This validation example has been updated to reflect the possibility of up to 245 known ISO country codes in addition to the regular extensions of email addresses.

Code
Select All
Option Explicit
Public EMsg As String 'used to return potential error messages in the functions below

Public Function ValidateEmail(ByVal strEmail As String) As Boolean
Dim strTmp As String, n As Long, sEXT As String
EMsg = "" 'reset on open for good form
ValidateEmail = True 'Assume true on init

sEXT = strEmail
Do While InStr(1, sEXT, ".") <> 0
   sEXT = Right(sEXT, Len(sEXT) - InStr(1, sEXT, "."))
Loop

If strEmail = "" Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>You did not enter an email address!"
ElseIf InStr(1, strEmail, "@") = 0 Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>Your email address does not contain an @ sign."
ElseIf InStr(1, strEmail, "@") = 1 Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>Your @ sign can not be the first character in your email address!"
ElseIf InStr(1, strEmail, "@") = Len(strEmail) Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>Your @sign can not be the last character in your email address!"
ElseIf EXTisOK(sEXT) = False Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>Your email address is not carrying a valid ending!"
   EMsg = EMsg & "<BR>It must be one of the following..."
   EMsg = EMsg & "<BR>.com, .net, .gov, .org, .edu, .biz, .tv Or included country's assigned country code"
ElseIf Len(strEmail) < 6 Then
   ValidateEmail = False
   EMsg = EMsg & "<BR>Your email address is shorter than 6 characters which is impossible."
End If
strTmp = strEmail
Do While InStr(1, strTmp, "@") <> 0
   n = 1
   strTmp = Right(strTmp, Len(strTmp) - InStr(1, strTmp, "@"))
Loop
If n > 1 Then
   ValidateEmail = False 'found more than one @ sign
   EMsg = EMsg & "<BR>You have more than 1 @ sign in your email address"
End If
End Function


Public Function EXTisOK(ByVal sEXT As String) As Boolean
Dim EXT As String, X As Long
EXTisOK = False
If Left(sEXT, 1) <> "." Then sEXT = "." & sEXT
sEXT = UCase(sEXT) 'just to avoid errors
EXT = EXT & ".COM.EDU.GOV.NET.BIZ.ORG.TV"
EXT = EXT & ".AF.AL.DZ.As.AD.AO.AI.AQ.AG.AP.AR.AM.AW.AU.AT.AZ.BS.BH.BD.BB.BY"
EXT = EXT & ".BE.BZ.BJ.BM.BT.BO.BA.BW.BV.BR.IO.BN.BG.BF.MM.BI.KH.CM.CA.CV.KY"
EXT = EXT & ".CF.TD.CL.CN.CX.CC.CO.KM.CG.CD.CK.CR.CI.HR.CU.CY.CZ.DK.DJ.DM.DO"
EXT = EXT & ".TP.EC.EG.SV.GQ.ER.EE.ET.FK.FO.FJ.FI.CS.SU.FR.FX.GF.PF.TF.GA.GM.GE.DE"
EXT = EXT & ".GH.GI.GB.GR.GL.GD.GP.GU.GT.GN.GW.GY.HT.HM.HN.HK.HU.IS.IN.ID.IR.IQ"
EXT = EXT & ".IE.IL.IT.JM.JP.JO.KZ.KE.KI.KW.KG.LA.LV.LB.LS.LR.LY.LI.LT.LU.MO.MK.MG"
EXT = EXT & ".MW.MY.MV.ML.MT.MH.MQ.MR.MU.YT.MX.FM.MD.MC.MN.MS.MA.MZ.NA"
EXT = EXT & ".NR.NP.NL.AN.NT.NC.NZ.NI.NE.NG.NU.NF.KP.MP.NO.OM.PK.PW.PA.PG.PY"
EXT = EXT & ".PE.PH.PN.PL.PT.PR.QA.RE.RO.RU.RW.GS.SH.KN.LC.PM.ST.VC.SM.SA.SN.SC"
EXT = EXT & ".SL.SG.SK.SI.SB.SO.ZA.KR.ES.LK.SD.SR.SJ.SZ.SE.CH.SY.TJ.TW.TZ.TH.TG.TK"
EXT = EXT & ".TO.TT.TN.TR.TM.TC.TV.UG.UA.AE.UK.US.UY.UM.UZ.VU.VA.VE.VN.VG.VI"
EXT = EXT & ".WF.WS.EH.YE.YU.ZR.ZM.ZW"
EXT = UCase(EXT) 'just to avoid errors
If InStr(1, sEXT, EXT, vbBinaryCompare) <> 0 Then EXTisOK = True
End Function  

Source is  http://www.developerfusion.com/show/3350/

====================================

And a third version that uses REGULAR EXPRESSIONS:
Code
Select All
<script language="vbscript">
function ValidateEmail(sEmail)
    set myExpression = new RegExp
    myExpression.pattern = "^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$"
    If myExpression.test(sEmail.value) = True Then
	msgbox "Valid Email"
    Else
	msgbox "Invalid Email"
    End If
End Function
</script> 

Source is http://www.devx.com/tips/Tip/15306

===================================
These can at least give you an idea of some of the approaches that others have taken.

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
addison1
Member
*
Offline


No personal text

Posts: 43
Joined: Aug 1st, 2004
Re: verify format of e-mail address
Reply #2 - Jun 8th, 2005 at 9:36pm
Print Post Print Post  
Bob,
Thanks for sharing all the hard work, I don't know that I'm smart enough to use your ideas.
Addison1
  
Back to top
 
IP Logged
 
addison1
Member
*
Offline


No personal text

Posts: 43
Joined: Aug 1st, 2004
Re: verify format of e-mail address
Reply #3 - Jun 8th, 2005 at 9:46pm
Print Post Print Post  
Bob,
I looked at your stuff again, and maybe I am smart enough.
I think that a combination of @left, @right, etc and  @instr will do almost everything that you've done.
I need to think about the valid extension thing a little more.   
Thanks again
Addison1
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: verify format of e-mail address
Reply #4 - Jun 8th, 2005 at 11:58pm
Print Post Print Post  
Note the sources on the code I provided.  Not my stuff, but stuff I have borrowed.  Good luck.

I agree that this can all be done. 

We are all awaiting your CheckEmailFormat() function for our libraries.

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged