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; |