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

Wednesday, March 21, 2012

Dynamic Field Visibility Control

This is a development best practice. You might already know this, however for those people who never used it; this will become handy for you.

There is a configuration page and you want to define what all fields need to be visible or invisible based on certain key values. The field names are stored in the physical table.

Then on the target page, do a CreateSQL and get the field names with the key values passed to it. Then in the while loop you can use the code snippet below. And the requirement is done.

&Field_Control = CreateSQL("”YOUR SQL”);
While &Field_Control.fetch(&Field_Name)
&record.GetField(@("field." | &Field_Name)).Visible = False/True;
End-While;

@ is the game setter here :)

Monday, March 5, 2012

Get a value from the XML

Okay so this is the scenario. You've got a response from the XML request you places via Integration Broker and you want to extract the data from a specific tag for further processing. The below code will help you to accomplish the same.

&lmsgGoRequest = CreateMessage(Operation.OPERATION_NAME, %IntBroker_Request);
&lmsgGoRequest.SetXmlDoc(&lxmlDoc);
&lmsgGoResponse = %IntBroker.SyncRequest(&lmsgGoRequest);
&MyDoc = CreateXmlDoc(&lmsgGoResponse.GenXMLString());
&field1List = &MyDoc.GetElementsByTagName("EMPLID");
WinMessage(&field1List [1].NodeValue);

&field1List is the array of the tags fetched from the XML (which you have specified) and NodeValue gives you the value of the tag.

Hope it helps!