Script Examples: Calculate the Check Digit
Calculating the check digit can be performed either using a Script data source or a Formula data source. The example below shows how to perform this task using a Script data source. Data map values are retrieved to perform the calculation and then the field data member is set at the end. This assumes that no other data source will be setting the data for this field at run time.
Note: When a form that includes a check digit is used, the Data Provider Person or process that enters data into a form or other data entry view for a label that was configured by a Designer. A user acting as a Data Provider requires the DOCUMENT_PRINTER role or equivalent permissions. is not alerted if the check digit fails.
var result;
var input = new String;
var cf = MAP.getStringValue("/Body/Collection_Facility");
var cy = MAP.getStringValue("/Body/Collection_Year");
var sn = MAP.getStringValue("/Body/Seq_No");
input = cf + cy + sn;
var ch, sum=0, charValue, isDigit, count;
var CharTable="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*";
var inputLength = input.length;
for (count = 0; count < inputLength; count++){
ch = input.charAt(count);
isDigit = ((ch >= "0") && (ch <="9"));
if(isDigit){
charValue = ch.charCodeAt(0) - "0".charCodeAt(0);
}else{
if(ch==ch.toLowerCase()) {
ch = ch.toUpperCase();
}
charValue = ch.charCodeAt(0) - "A".charCodeAt(0) + 10;
}
sum = ((sum + charValue) * 2) % 37;
}
charValue = (38 - sum) % 37;
result = CharTable[charValue];
MAP.setValueForKey("/Body/Check_Character",result);