Validates e-mail address - Written in Progress/Openedge 4GL

/* This function is written en Progress 4GL 9.1C */ /* It receives a character containing an e-mail address */ /* and validates it, returning a logical value TRUE if */ /* the address is acceptable, and FALSE if it isn't */ /* Ezequiel Montoya Saldaña - Lima - Perú - 2016 */ RETURNS LOGICAL ( cMail AS CHARACTER ) : DEF VAR nChar AS INTEGER NO-UNDO. DEF VAR nAT AS INTEGER NO-UNDO. cMail = TRIM(cMail). IF (LENGTH(cMail) < 5 OR LENGTH(cMail) > 320) THEN /* Minimal acceptable email: X@X.X */ RETURN FALSE. IF INDEX(".." , cMail) <> 0 OR index("@" , cMail) = 0 THEN RETURN FALSE. DO nChar = 1 TO LENGTH(cMail) : IF INDEX("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_@" , CAPS(SUBSTRING(cMail,nChar,1))) = 0 THEN RETURN FALSE. IF INDEX("@" , CAPS(SUBSTRING(cMail,nChar,1))) <> 0 THEN DO: nAt = nAt + 1. IF nAt > 1 THEN RETURN FALSE. END. END. RETURN TRUE. /* Function return value. */ END FUNCTION.
This function is written en Progress 4GL 9.1C

It receives a character containing an e-mail address and validates it, returning a logical value TRUE if the address is acceptable, and FALSE if it isn't.

10/13/2016 Added some validations:
Maximum length of cMail
Just one "@" accepted (Thanks to @Maria Gonzalez)
At least one "@" is necessary
".." not accepted

4 Responses

Hola!
I tried using your function but I am able to add "@@@@@@@@" as email address. Just a heads up!
@Maria Gonzalez That's a good one!
I'll see what I can do, thanks.
I did this. Maybe it is too rigid...
(based on your snippet, I just changed the variable names).



function validateemail returns logical ( v-email as character ) :

define variable nChar as integer.
define variable v-length as integer.
define variable v-left as character format "x(50)" no-undo .
define variable v-right as character format "x(50)" no-undo .
define variable v-at as integer.
define variable v-dot as integer.
v-email = trim(v-email).
v-length = length(v-email).
if v-length< 5 then /* Minimal acceptable email: X@X.X */
return false.

v-at = index(v-email, "@").
v-left = substring (v-email, 1, (v-at - 1)).
v-right = substring(v-email, (v-at + 1), (v-length - (v-at ))).
v-dot = index(v-right,".").
display v-left.
display v-right.



if v-at = 0 or v-dot = 0 or length(v-left) = 0 or length(v-right) = 0 then
do:
return false.
end.


do nChar = 1 to length(v-left) :
if index("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_" , caps(substring(v-left,nChar,1))) = 0 then
do:
return false.
end.
end.
nChar = 0.
do nChar = 1 to length(v-right) :
if index("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-" , caps(substring(v-right,nChar,1))) = 0 then
do:
return false.
end.
end.

return true. /* Function return value. */

end function.
@Maria Gonzalez Hi, I just added validations including the "@@@" thing (thank to you).
Your version checks the length of the previous-@ and post-@ sections and that's a good thing.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.