• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Luhn Algorithm For Credit Card Validation

Explorer ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

Hello,

I am trying to find a code that uses the Luhn Algorithm in a editable PDF file to confirm that a credit card number is valid before saving the file.

I'm not finding anything that is specific to a cc number, though I did find something useful that uses the algorithm to validate a Swedish ss number.

I don't think I know that right questions to ask to generate guidance on the internet.

Any assistance that can be provided would be most appreciated.

Thank you in advance!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

63

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

If it's the same algorithm as the one used for the Swedish SS# you found, then it should work for a CC#, too.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2021 Aug 13, 2021

Copy link to clipboard

Copied

LATEST

That is what I was thinking, the issue I think, is that I don't know how to read it so I don't know how to adjust it.  It is much more advanced that anything I've attempted so far...

if (event.value) {
	var result = validatePersonnummer(event.value);
	if (result==false) {
		app.alert("Invalid number!");
		event.rc = false;
	}
}

function validatePersonnummer(input) {
    // Check valid length & form
    if (!input) return false;

    if (input.indexOf('-') == -1) {
        if (input.length === 10) {
            input = input.slice(0, 6) + "-" + input.slice(6);
        } else {
            input = input.slice(0, 8) + "-" + input.slice(8);
        }
    }
    if (!input.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})|(\d{4})(\d{2})(\d{2})\-(\d{4})$/)) return false;

    // Clean input
    input = input.replace('-', '');
    if (input.length == 12) {
        input = input.substring(2);
    }

    // Declare variables
    var d = new Date(((!!RegExp.$1) ? RegExp.$1 : RegExp.$5), (((!!RegExp.$2) ? RegExp.$2 : RegExp.$6)-1), ((!!RegExp.$3) ? RegExp.$3 : RegExp.$7)),
            sum = 0,
            numdigits = input.length,
            parity = numdigits % 2,
            i,
            digit;

    // Check valid date
    if (Object.prototype.toString.call(d) !== "[object Date]" || isNaN(d.getTime())) return false;

    // Check luhn algorithm
    for (i = 0; i < numdigits; i = i + 1) {
        digit = parseInt(input.charAt(i))
        if (i % 2 == parity) digit *= 2;
        if (digit > 9) digit -= 9;
        sum += digit;
    }
    return (sum % 10) == 0;
};

 

I know that I need to adjust the number of numbers it is reading...but I have no idea which numbers to adjust.... 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines