Saturday, January 29, 2011

Validating with Format Specified in Setup Table. (Example: National ID validation Based On Country)

The below code can be used for validating data with specified Format in Setup Table.

For Example:

In Add Person page (or Modify Person page), National ID validation is based on country and each national id has its own format. So hard coding of format is not possible, it should be generic to convert input data to specified format and validate with the Format specified in setup page.
For National ID validation, National ID Type setup page is used for setting up National ID Type and its format.
Page Navigation: Main Menu> Setup HRMS > Foundation Tables > Personal > National ID Type.



Following  code can be used for Converting input data to specified format and validate with the Format.

Code:

/******************************************************
Function : CheckNID_General
Purpose : Check format of NATIONAL_ID against definition in N_NAT_ID_TYPE
Parameters : &NATIONAL_ID, &NID_TYPE, &COUNTRY
Returns :
******************************************************/
Function CheckNID_General(&NATIONAL_ID, &NID_TYPE, &COUNTRY, &NIDFormat);
Local string &Alpha, &Alpha_xz, &Numeric, &TestChar, &MaskChar;
Local number &iNID, &LengthNID;
Local boolean &IsFormatError;
&Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
&Alpha_xz = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
&Numeric = "1234567890";
&IsFormatError = False;
If All(&NATIONAL_ID) Then
If All(&NIDFormat) Then
&LengthNID = Len(&NIDFormat);
If Len(&NATIONAL_ID) > &LengthNID Then
/* Incorrect Length */
&IsFormatError = True;
End-If;
&K = 0;
For &J = 1 To &LengthNID
&ChkChar = Substring(&NIDFormat, &J, 1);
If &ChkChar = "X" Or
&MaskChar = "Z" Then
&K = &K + 1;
End-If;
End-For;
If &K <> &LengthNID Then
&iNID = 1;
While &iNID <= &LengthNID And
Not &IsFormatError
&TestChar = Substring(&NATIONAL_ID, &iNID, 1);
&MaskChar = Substring(&NIDFormat, &iNID, 1);
Evaluate &MaskChar
When = "X"
When = "Z"
/* Any Alpha or Numeric character is acceptable */
If Find(&TestChar, &Numeric) <> 0 Or
Find(&TestChar, &Alpha_xz) <> 0 Then
&IsFormatError = False;
Else
&IsFormatError = True;
End-If;
Break;
When = "9"
/* Must be Numeric */
If Find(&TestChar, &Numeric) = 0 Then
&IsFormatError = True;
End-If;
Break;
When = "A"
/* Must be Alpha */
If Find(&TestChar, &Alpha) = 0 Then
&IsFormatError = True;
End-If;
Break;
When-Other
/* Special: must be equal to the mask character */
If &TestChar <> &MaskChar Then
&IsFormatError = True;
End-If;
Break;
End-Evaluate;
&iNID = &iNID + 1;
End-While;
End-If;
If &IsFormatError Then
/* "The format of the National ID Type %1 for country %2 has to be %3." */
Error (MsgGet(1000, 490, "Message Not Found", &NID_TYPE, &COUNTRY, &NIDFormat));
End-If;
End-If;
End-If;
End-If;
End-Function;
/**********************************************************************/

/******************************************************
Function : CheckNID
Purpose : Main Check of National ID
Parameters : &NID_VRBL, &NID_TYPE, &COUNTRY
Returns :
******************************************************/
Function Check_NID(&NID_VRBL, &NID_TYPE, &COUNTRY);
&NATIONAL_ID = &NID_VRBL;
/* if the NID entered is shorter than its format, we assume an entry without special chars; thus we insert the special chars prior to checking the NID format */
If All(&NATIONAL_ID) Then
SQLExec("Select NATIONAL_ID_FORMAT from PS_NAT_ID_TBL Where N_COUNTRY=:1 AND N_NATIONALIDTYPE =:2", &COUNTRY, &NID_TYPE, &NATIONAL_ID_FORMAT);
<*
If Not None(&NATIONAL_ID_FORMAT) Then
&LENGTH = Len(&NATIONAL_ID_FORMAT);
If Len(&NATIONAL_ID) <= &LENGTH Then
&NATIONAL_ID = Format_NID(&COUNTRY, &NID_TYPE, &NATIONAL_ID, "OUTOFDB");
End-If;
End-If;
*>
End-If;
/*******************/
/* General Check */
/*******************/
CheckNID_General(&NATIONAL_ID, &NID_TYPE, &COUNTRY, &NATIONAL_ID_FORMAT);
END-FUNCTION;
/**********************************************************************/

3 comments:

llori998 said...

Thanks for sharing this! Do you happen to have a list of all the countries and their national id format requirements?

Pawan Mundhra said...

You are welcome. Right now we don't have all countries and their national id formats but major of them are captured on setup tables.

Unknown said...

If you have '-' in the NID format and the data was stored without '-', then this CheckNID_general function would return an error for the incorrect format!

Post a Comment