-
Notifications
You must be signed in to change notification settings - Fork 1
PostNet
Calculates the check digit for a supplied and valid Zip Code, Zip+4 or Zip+4 DPBC and formats the value for barcode font usage.
SpecName (constant) The variable containing the input to be converted. Proper EAN13 font must be applied in the template.
AlternateStartStopChar (multiple | optional) Optional value for stop and start character required by the barcode font. The default is ! and required by the Four51 recommended PostNet font.
ErrorMessage (multiple | optional) The error message to return when any error occurs.
This method follows the specifications set forth by the United States Postal Service. Within the method a test of the check digit calculation is performed. If the test fails an error will be returned to avoid any inaccurate barcodes from being rendered to the page. The "-" character is also replaced automatically. For example: "55555-1237" will be returned as "!5555512372!".
public static void PostNet(string SpecName,object AlternateStartStopChar, object ErrorMessage)
{
Break();
try
{
int total = 0;
int remainder = 0;
int checkdigit = 0;
string postcode = Variable(SpecName).Value.Replace("-", "");
IEnumerator chars = postcode.GetEnumerator();
while (chars.MoveNext())
{
total += Convert.ToInt32(chars.Current.ToString());
}
remainder = total % 10;
checkdigit = 10 - remainder;
if ((total + checkdigit) % 10 != 0)
throw new Exception("The calculation failed proofing");
Variable(SpecName).Value = String.Format("{2}{0}{1}{2}",
postcode,
checkdigit.ToString(),
AlternateStartStopChar.ToString().Length > 0 ? AlternateStartStopChar.ToString() : "!");
}
catch (Exception ex)
{
throw new Four51ActionsException(ex.Message, (string)ErrorMessage);
}
}