Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

E-mail validation 3

Status
Not open for further replies.

jimoo

Programmer
Jun 2, 2003
1,111
US
I want to write a routine for email validation.

Here is what I have come up with based on a sample email of:

Sample@sample.com

1. Make sure there is only one @ in the name.
2. Make sure there are characters before and after the @
3. Make sure there is a period after the @

Any other ideas or sample snippets are greatly appreciated



Jim Osieczonek
Delta Business Group, LLC
 
Thanks Dave,

Unfortunately, that thread did not help much. I was unable to find the MSDN document that listed the business rules for email. It simply brought me to MSDN. I searched through it, including "email" in the search box and never found the rules.

The other snippet of code in that thread was from the person submitting the question (i.e. how to validate e-mails) and although somewhat helpful, it still did not define the rules.

I did a google search and found the following article:

Here is the code I implemented as a result:
Code:
LPARAMETERS tcEmail
IF PCOUNT() < 1 OR TYPE(tcEmail) <> "C"
	RETURN .F.
ENDIF	

* article to reference: 
* [URL unfurl="true"]http://www.developer.com/lang/php/article.php/3290141[/URL]


tcEmail = ALLTRIM(tcEmail)

LOCAL llValid 
llValid = .T.

DO CASE 
CASE OCCURS("@",tcEmail) <> 1
	llValid = .F.
CASE ATC("@",tcEmail) = LEFT(tcEmail,1) OR ATC("@",tcEmail) = RIGHT(tcEmail,1)
	llValid = .F.
CASE ATC(".",tcEmail) = LEFT(tcEmail,1) OR ATC(".",tcEmail) = RIGHT(tcEmail,1)
	llValid = .F.
CASE ATC("_",tcEmail) = LEFT(tcEmail,1) OR ATC("_",tcEmail) = RIGHT(tcEmail,1)
	llValid = .F.
CASE ATC("-",tcEmail) = LEFT(tcEmail,1) OR ATC("-",tcEmail) = RIGHT(tcEmail,1)
	llValid = .F.
ENDCASE

* make sure all characters are valid characters
FOR lni = 1 TO LEN(tcEmail)
	IF NOT UPPER(SUBSTR(tcEmail,lni,1)) $ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.@"
		llValid = .F.
		EXIT
	ENDIF
ENDFOR

* ending part of email, called TLD, must be between 2 and 4 characters and
* alphabetic
lnLen = LEN(tcEmail)
FOR lni = lnLen TO 1 STEP -1
	IF SUBSTR(tcEmail,lni,1) = "."
		IF NOT BETWEEN(lnLen-lni,2,4)
			llValid = .F.
		ENDIF
		EXIT
	ELSE
		IF NOT ISALPHA(SUBSTR(tcEmail,lni,1))
			llValid = .F.
			EXIT
		ENDIF	
	ENDIF
ENDFOR	

RETURN llValid

I don't know if this is perfect, but it is what I came up with from the article.

To all - feel free to add to this if I am missing something.



Jim Osieczonek
Delta Business Group, LLC
 
I didn't check my code close enough. Here is updated code after the orkin man killed the bugs:

Code:
LPARAMETERS tcEmail
IF PCOUNT() < 1 
	RETURN .F.
ENDIF	

* article to reference: 
* [URL unfurl="true"]http://www.developer.com/lang/php/article.php/3290141[/URL]


tcEmail = ALLTRIM(tcEmail)

LOCAL llValid 
llValid = .T.

DO CASE 
CASE OCCURS("@",tcEmail) <> 1
	llValid = .F.
CASE LEFT(tcEmail,1) $ "@._-" OR RIGHT(tcEmail,1) $ "@._-" 
	* these characters are acceptable, but not at beginning or end
	llValid = .F.
ENDCASE

* make sure all characters are valid characters
FOR lni = 1 TO LEN(tcEmail)
	IF NOT UPPER(SUBSTR(tcEmail,lni,1)) $ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.@"
		llValid = .F.
		EXIT
	ENDIF
ENDFOR

* ending part of email, called TLD, must be between 2 and 4 characters and
* alphabetic
lnLen = LEN(tcEmail)
FOR lni = lnLen TO 1 STEP -1
	IF SUBSTR(tcEmail,lni,1) = "."
		IF NOT BETWEEN(lnLen-lni,2,4)
			llValid = .F.
		ENDIF
		EXIT
	ELSE
		IF NOT ISALPHA(SUBSTR(tcEmail,lni,1))
			llValid = .F.
			EXIT
		ENDIF	
	ENDIF
ENDFOR	

RETURN llValid

Jim Osieczonek
Delta Business Group, LLC
 
Jim,

1. Make sure there is only one @ in the name.
2. Make sure there are characters before and after the @
3. Make sure there is a period after the @


Those rules will correctly trap the most obvious errors. You can refine them further, but it might not be worth the effort.

The formal definition of the valid email address syntax can be found in para. 3.4 of RFC 2822 (see Good luck with wading through it -- it's not the most readable of documents (it's not meant to be).

One small detail: According to the spec, the string to the left of the @ can have almost any ASCII characters you like, provided the string is enclosed in double-quotes. Strinctly speaking, that means that you could have an @ in the string to the left of the @, which means that your first rule won't work.

However, this is a very obscure point, and I doubt if you'll ever find an email address that follows that syntax, so you can probably safely ignore it. In any case, I expect that most mail servers apply their own validation rules which are probably similar to yours, so no-one would actually use such an obscure address. (But you never know.)

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
You could, if feeling extreme and if you're on Nt/2k/etc on an online network, add a check that the domain exists:

cFile= sys(3)
cCmd="!nslookup -type=MX " + cDomainname + " > " + cFile
&cCmd
? fileToString(cFile)
delete file cFile

and if the result includes the phrase "mail exchanger" then you've got a real mailserver.

Chris
 
Here is what I use:


LOCAL loRegExp
loRegExp = CreateObject("VBScript.RegExp")
loRegExp.IgnoreCase = .T.
loRegExp.Pattern = '^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$'
m.valid = loRegExp.Test(m.email)
RELEASE loRegExp
IF m.valid = .T.
....
ENDIF
 
Hi Mike,

I think it does all that.

The 1st & 2nd Case statement should take care of your item 1 & item 2 respecfully.

The period after the @ will get caught in the code that checks to see if the ending (TLD) is between 2 & 4 in length & alphabetic. This code is only executed if the @ only occurs one time. If the ending (TLD) is non-alpha (meaning it appears after the last period) it will get flagged for non-alpha.

In short - I think it handles those 3 requirements.

There is a reference to the link you provided in the link I provided. I looked at that document. Concur, very dull.



Jim Osieczonek
Delta Business Group, LLC
 
See Jim, if you'd come to our Regular Expression meeting a couple months ago, you'd know how good Reggie's answer is!
:)

Rick
 
RgBean & Reggie

I agree with you Rick, that answer is great. That is star worthy!

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 

That is star worthy!

Although at this point I cannot seems to be able to do that.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
You're probably right Rick. I can't say I full understand Reggie's reply.

I understand:
loRegExp.Pattern = '^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$'

I can pick part of it out, like the A-Za-z-09, but the rest is fuzzy. Reggie, or others, do you care to explain it in more detail?



Jim Osieczonek
Delta Business Group, LLC
 
It makes more sense now, but what I don't like about it is that is uses the windows scripting host instead of native fox language. For most users this is not an issue, but some administrators have opted to unistall the scripting host or to disable its features.

If you are developing for a multi-environment (like I am) and don't know what the status of the scripting host is, it may not be safe to assume the scripting host is loaded and enabled.



Jim Osieczonek
Delta Business Group, LLC
 
jimoo

For most users this is not an issue, but some administrators have opted to unistall the scripting host or to disable its features.

Then use the same pattern, but instead of Windows Scripting use the Regular Expression Foundation Class (...\Samples\Solution\Ffc\Regexp.scx ).


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike,
The VFP Regexp.VCX is really just a shell that calls the VBScript.RegExp object, so if WSH is totally disbled it won't matter if you do it directly or go through this VFP class!

Rick

 

Thanks Rick

I hadnt' really look into it. Good to know.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top