Monday, October 15, 2007

Credit Card number validation XPath expression

The next XPath expression validates credit-card numbers when they are in between 13-16 digits long:


(string-length(.) >= 13 and string-length(.) <= 16 and
if(string-length(.) mod 2 = 0,
translate(substring(., 1,1), '12345678', '24681357') + substring(., 2,1) +
translate(substring(., 3,1), '12345678', '24681357') + substring(., 4,1) +
translate(substring(., 5,1), '12345678', '24681357') + substring(., 6,1) +
translate(substring(., 7,1), '12345678', '24681357') + substring(., 8,1) +
translate(substring(., 9,1), '12345678', '24681357') + substring(., 10,1) +
translate(substring(., 11,1), '12345678', '24681357') + substring(., 12,1) +
translate(substring(., 13,1), '12345678', '24681357') +
if(string-length(.) > 13, substring(., 14,1), 0) +
if(string-length(.) > 14, translate(substring(., 15,1), '12345678', '24681357'), 0) +
if(string-length(.) > 15, substring(., 16,1), 0)
,
substring(., 1,1) + translate(substring(., 2,1), '12345678', '24681357') +
substring(., 3,1) + translate(substring(., 4,1), '12345678', '24681357') +
substring(., 5,1) + translate(substring(., 6,1), '12345678', '24681357') +
substring(., 7,1) + translate(substring(., 8,1), '12345678', '24681357') +
substring(., 9,1) + translate(substring(., 10,1), '12345678', '24681357') +
substring(., 11,1) + translate(substring(., 12,1), '12345678', '24681357') +
substring(., 13,1) +
if(string-length(.) > 13, translate(substring(., 14,1), '12345678', '24681357'), 0) +
if(string-length(.) > 14, substring(., 15,1), 0) +
if(string-length(.) > 15, translate(substring(., 16,1), '12345678', '24681357'), 0)
) mod 10 = 0)


I'm using it in an XForms application, mimicking the 1.1 luhn() function, which would perform the same task.

1 comment:

Tavia said...

Good for people to know.

Related Posts Plugin for WordPress, Blogger...