I came across
this article on validating email addresses, and decided to convert
the code into AHK.
Here it is. Please test.
Code:
#SingleInstance force
/*
Valid Email RegEx?
http://www.pgregg.com/projects/php/code/showvalidemail.php
http://www.pgregg.com/projects/php/code/validate_email.inc.phps
*/
emailTest =
(LTrim % Join`n
name.lastname@domain.com|true
.@|false
a@b|false
@bar.com|false
@@bar.com|false
a@bar.com|true
aaa.com|false
aaa@.com|false
aaa@.123|false
aaa@[123.123.123.123]|true
aaa@[123.123.123.123]a|false
aaa@[123.123.123.333]|false
a@bar.com.|false
a@bar|false
a-b@bar.com|true
+@b.c|false
+@b.com|true
a@-b.com|false
a@b-.com|false
-@..com|false
-@a..com|false
a@b.co-foo.uk|true
"hello my name is"@stutter.com|true
"Test \"Fail\" Ing"@example.com|true
valid@special.museum|true
invalid@special.museum-|false
shaitan@my-domain.thisisminekthx|false
test@...........com|false
foobar@192.168.0.1|false
"Abc\@def"@example.com|true
"Fred Bloggs"@example.com|true
"Joe\\Blow"@example.com|true
"Abc@def"@example.com|true
customer/department=shipping@example.com|true
$A12345@example.com|true
!def!xyz%abc@example.com|true
_somename@example.com|true
Test \\'.chr(10).' Folding \\'.chr(10).' Whitespace@example.com|true
HM2Kinsists@(that comments are allowed)this.is.ok|true
user%uucp!path@somehost.edu|true
)
Loop, Parse, emailTest, `n
{
StringSplit, emailTestArray, A_LoopField, |
isit := isValidEmail(emailTestArray1)
;If (emailtestarray2 != resArray1) ; error
MsgBox,, Testing,
(LTrim
Email: %emailTestArray1%
Should be: %emailTestArray2%
Is reported as: %isit%
)
}
MsgBox Done testing
Return
isValidEmail(emailstr)
{
; Get length
emailstr_len := StrLen(emailstr)
; Remove whitespace (AutoTrim)
emailstr = %emailstr%
; Make lowercase
StringLower, emailstr, emailstr
; Split it up into before and after the @ symbol
StringGetPos, atPos, emailstr, @, R
If ErrorLevel
Return false ; no @
StringLeft, local_part, emailstr, %atPos%
StringRight, domain_part, emailstr, % emailstr_len - atPos - 1
; Sanitize quoted parts
local_part := RegExReplace(local_part, "\\\.", "_")
local_part := RegExReplace(local_part, """[^""]+""", ".")
; Comments ( this is a comment ) are permitted in domain parts
domain_part := RegExReplace(domain_part, "\([^()]*\)", "")
; Make sure there are no more @ (we sanitized valid ones above)
If InStr(local_part, "@")
Return false ; too many @
; Check that the username is >= 1 char
If StrLen(local_part) = 0
Return false ; username missing
; Split the domain part into the dotted parts
StringSplit, domain_components, domain_part, `.
; Check there are at least 2
If domain_components0 < 2
Return false ; not enough domain components
; Check each domain part to ensure it doesn't start or end with a bad char
Loop %domain_components0%
{
domain_component := domain_components%A_Index%
If (StrLen(domain_component) > 0)
{
StringLeft, firstChar, domain_component, 1
StringRight, lastChar, domain_component, 1
If RegExMatch(firstChar, "[\.-]") Or RegExMatch(lastChar, "[\.-]")
Return false ; wrong start/end character in domain component
}
Else
Return false ; domain component missing
}
; Check the last domain component has 2-6 chars (.uk to .museum)
domain_last := domain_components%domain_components0%
If (StrLen(domain_last) < 2) Or If (StrLen(domain_last) > 6)
Return false ; TLD too large or small
; Check for valid chars - Domains can only have A-Z, 0-9, ., and the - chars,
; or be in the form [123.123.123.123]
If RegExMatch(domain_part, "^\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]$")
{
If ip2long(domain_part) != 0
Return true ; ip
Else
Return false ; ip error
}
If RegExMatch(domain_part, "^[a-z0-9\.-]+$")
Return true ; domain
; If we get here then it didn't pass
Return false ; end of function
}
ip2long(ip)
{ ; http://www.cflib.org/udf/ip2long
ip := RegExReplace(ip, "\[|\]", "") ; remove xtra chars
StringSplit, iparr, ip, `.
If (iparr0 != 4)
Return False
If (iparr1 > 255 Or iparr2 > 255 Or iparr3 > 255 Or iparr4 > 255)
Return False
Else
Return (iparr1*256^3) + (iparr2*256^2) + (iparr3*256) + iparr4
}
Edit: Clarified demo a bit more (I hope), and some changes.