Search This Blog

Tuesday, March 27, 2012

Email Address Validation Function

Did some changes in the delivered code. Page fields were hard coded in the delivered code, made it dynamic.

Function validateEmailAddressEntry(&Email_Addr As string, &Page_Field As string)

Local string &Email_After@, &str_Host, &local_part, &Domain_part;
Local number &Loc@, &Loc_dot, &next_dot, &local_LENGTH, &Domain_length, &LOOP;
Local string &ALPHA_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-";
Local string &char_String = "#$%&'*+/=?^_`{|}~";
Local string &NUM_STRING = "1234567890";

&Email_Addr = RTrim(LTrim(&Email_Addr));

If All(&Email_Addr) Then
&Loc@ = Find("@", &Email_Addr);
&Loc_dot = Find(".", &Email_Addr);

&local_part = Substring(&Email_Addr, 1, &Loc@ - 1);
&Domain_part = Substring(&Email_Addr, &Loc@ + 1, Len(&Email_Addr) - &Loc@);
&local_LENGTH = Len(&local_part);
&Domain_length = Len(&Domain_part);
&Email_After@ = Substring(&Email_Addr, &Loc@ + 1, Len(&Email_Addr) - &Loc@);

/* The e-mail address must contain @ and . characters */
If &Loc@ = 0 Or
&Loc_dot = 0 Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 895, "The e-mail address must contain @ and . characters");
End-If;

/* The e-mail address shall not contain @ or . at the first character. */
If &Loc@ = 1 Or
&Loc_dot = 1 Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 896, "The e-mail address shall not contain @ or . at the first character.");
End-If;

/* Two at signs (@) are not allowed */
If (Find("@", &Email_After@) > 0) Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 899, "Two at signs (@) are not allowed");
End-If;

/* The e-mail address shall not contain - as the first character in Domain name. */
If (Find("-", &Email_After@) = 1) Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 900, "The e-mail address shall not contain - as the first character in Domain name.");
End-If;

/* The e-mail address shall contain atleast one character between @ and . */
If ((Substring(&Email_Addr, &Loc@ - 1, 1) = ".") Or
(Substring(&Email_Addr, &Loc@ + 1, 1) = ".")) Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 901, "The e-mail address shall contain atleast one character between @ and .");
End-If;

/* The e-mail address shall not contain @ or . at the last character. */
If ((Substring(&Email_Addr, Len(&Email_Addr), 1) = ".") Or
(Substring(&Email_Addr, Len(&Email_Addr), 1) = "@")) Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 897, "The e-mail address shall not contain @ or . at the last character.");
End-If;

If (&Loc_dot > 1) Then

/* */
&str_Host = Substring(&Email_Addr, &Loc_dot + 1, Len(&Email_Addr) - &Loc_dot);
&next_dot = Find(".", &str_Host);

While &next_dot > 0
If &next_dot = 1 Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 902, "The e-mail address shall contain atleast one character between . and .");
Break;
Else
&Loc_dot = &next_dot;
&Email_After@ = &str_Host;
&str_Host = Substring(&str_Host, &next_dot + 1, Len(&str_Host) - &next_dot);
&next_dot = Find(".", &str_Host);
End-If;
End-While;

If Len(&str_Host) < 2 Then SetCursorPos(%Page, @(&Page_Field)); Error MsgGet(14100, 903, "The e-mail address should contain atleast two characters after last ."); End-If; End-If; /* Validate that the local part consists of the ascii characters are described above for local part */ &LOOP = 1; While &LOOP <= &local_LENGTH /* check for alpha in the format */ If ((Find(Substring(&local_part, &LOOP, 1), &ALPHA_STRING) = 0) And (Find(Substring(&local_part, &LOOP, 1), &NUM_STRING) = 0) And (Find(Substring(&local_part, &LOOP, 1), &char_String) = 0)) Then SetCursorPos(%Page, @(&Page_Field)); Warning MsgGet(14100, 904, "Invalid character found in local part of email address."); Break; End-If; &LOOP = &LOOP + 1; End-While; /* Validate that domain name consists letters, digits and hyphens separated by dots */ &LOOP = 1; While &LOOP <= &Domain_length /* check for alpha in the format */ If ((Find(Substring(&Domain_part, &LOOP, 1), &ALPHA_STRING) = 0) And (Find(Substring(&Domain_part, &LOOP, 1), &NUM_STRING) = 0)) Then Warning MsgGet(14100, 905, "Invalid character found in domain part of email address."); Break; End-If; &LOOP = &LOOP + 1; End-While; /* Validate that the local part consists of at least 1 character before the @ sign but not more than 64 characters*/ If &local_LENGTH < 1 Or &local_LENGTH > 64 Then
SetCursorPos(%Page, @(&Page_Field));
Warning MsgGet(14100, 894, "Length of Local part of e-mail address before the @ sign must be >=1 and <= 64.");
End-If;

/* Make sure that atleast two characters are present between @ and . */
&Email_After@ = Substring(&Email_Addr, &Loc@ + 1, Len(&Email_Addr) - &Loc@);
&Loc_dot = Find(".", &Email_After@);
If (&Loc_dot = 0) Then
SetCursorPos(%Page, @(&Page_Field));
Error MsgGet(14100, 895, "The e-mail address must contain @ and . characters");

End-If;
End-If;

End-Function;

>> Tino Simon

No comments:

Post a Comment