I have a field coming down into a crystal report as a string and would like to check to see if it's a 16-digit credit card number. If it is, I need to mask the inner 8 characters of the credit card number with X's.
The credit card # may have spaces in it, or may not have any spaces in it.
Eg: 1234 5678 9012 3456
Goes to: 1234XXXXXXXX3456
My idea was to use the string Replace function and replace all spaces with an empty string if there are any. Then check to see the Length of the resultant string is 16 using Length and that they are all digits using IsNumeric or NumericText. Here is the code I wrote so far:
//Formula field:
StringVar str;
str := "N/A";
IF {TableName.StringFieldInQuestion} <> "0"
THEN
str := {TableName.StringFieldInQuestion};
StringVar ccCheck := Replace(str," ","");
IF Len(ccCheck) = 16 AND NumericText(ccCheck)
THEN
str := ccCheck[1 to 4] + "XXXXXXXX" + ccCheck[13 to 16]
str;
However, this code is not working. It strangely has some outputs for the string still having spaces in them, and other times does not work at all (masks the wrong characters or displays 5 characters at the end instead of 4). What am I doing wrong and is there a better practice for doing this? Thank you for any help, it is greatly appreciated.