By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

Invoice check number

P: n/a
I need to create a valid mod10 or luhn number that can be attached to
an invoice, I have found several examples showing how to validate a
credit card number or validate a number using mod10/luhn.

Example:
invoice number = 700123
invoice check number 700123(checknumber)

that check number should make the entire invoice number able to
validate using mod10/luhn

Mar 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Kris" <Kr*************@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
I need to create a valid mod10 or luhn number that can be attached to
an invoice, I have found several examples showing how to validate a
credit card number or validate a number using mod10/luhn.

Example:
invoice number = 700123
invoice check number 700123(checknumber)

that check number should make the entire invoice number able to
validate using mod10/luhn

Google is your friend.

I cobbled this together from several sources; will it help?

Validate it thoroughly before relying on it!
<?php
$inv = "700123";
echo $inv . checkdigit($inv);

function checkdigit($num) {
/*
* 1. Reverse the number
* 2. Multiply all the digits in odd positions (The first digit, the third
digit, etc) by 2.
* 3. If any one is greater than 9 subtract 9 from it.
* 4. Sum those numbers up
* 5. Add the even numbered digits (the second, fourth, etc) to the number
you got in the previous step
* 6. The check digit is the amount you need to add to that number to make
a multiple of 10.
* So if you got 68 in the previous step the check digit would be 2.
* You can calculate the digit in code using checkdigit = ((sum / 10 +
1) * 10 - sum) % 10
* -- http://www.darkcoding.net/index.php/.../luhn-formula/
*/
$sum = 0;
$pos = 0;
$rev = strrev($num);
$len = strlen($num);
if ($len % 2 == 0) $len += 1;
while ($pos < $len) {
$odd = $rev[$pos] * 2;
if ($odd 9) {
$odd -= 9;
}
$sum += $odd;
if ($pos != ($len - 2)) {
$sum += $rev[$pos +1];
}
$pos += 2;
}
return ((floor($sum/10) + 1) * 10 - $sum) % 10;
}
?>
Mar 1 '07 #2

P: n/a
On Mar 1, 11:42 am, "McKirahan" <N...@McKirahan.comwrote:
"Kris" <Kristian.Nis...@gmail.comwrote in message

news:11**********************@j27g2000cwj.googlegr oups.com...
I need to create a valid mod10 or luhn number that can be attached to
an invoice, I have found several examples showing how to validate a
credit card number or validate a number using mod10/luhn.
Example:
invoice number = 700123
invoice check number 700123(checknumber)
that check number should make the entire invoice number able to
validate using mod10/luhn

Google is your friend.

I cobbled this together from several sources; will it help?

Validate it thoroughly before relying on it!

<?php
$inv = "700123";
echo $inv . checkdigit($inv);

function checkdigit($num) {
/*
* 1. Reverse the number
* 2. Multiply all the digits in odd positions (The first digit, the third
digit, etc) by 2.
* 3. If any one is greater than 9 subtract 9 from it.
* 4. Sum those numbers up
* 5. Add the even numbered digits (the second, fourth, etc) to the number
you got in the previous step
* 6. The check digit is the amount you need to add to that number to make
a multiple of 10.
* So if you got 68 in the previous step the check digit would be 2.
* You can calculate the digit in code using checkdigit = ((sum / 10 +
1) * 10 - sum) % 10
* --http://www.darkcoding.net/index.php/credit-card/luhn-formula/
*/
$sum = 0;
$pos = 0;
$rev = strrev($num);
$len = strlen($num);
if ($len % 2 == 0) $len += 1;
while ($pos < $len) {
$odd = $rev[$pos] * 2;
if ($odd 9) {
$odd -= 9;
}
$sum += $odd;
if ($pos != ($len - 2)) {
$sum += $rev[$pos +1];
}
$pos += 2;
}
return ((floor($sum/10) + 1) * 10 - $sum) % 10;}

?>
looks like its doing the job, great.
Mar 1 '07 #3

P: n/a
Nel

"McKirahan" <Ne**@McKirahan.comwrote in message
news:4q******************************@comcast.com. ..
"Kris" <Kr*************@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>I need to create a valid mod10 or luhn number that can be attached to
an invoice, I have found several examples showing how to validate a
credit card number or validate a number using mod10/luhn.

Example:
invoice number = 700123
invoice check number 700123(checknumber)

that check number should make the entire invoice number able to
validate using mod10/luhn


Google is your friend.

I cobbled this together from several sources; will it help?

Validate it thoroughly before relying on it!
<?php
$inv = "700123";
echo $inv . checkdigit($inv);

function checkdigit($num) {
/*
* 1. Reverse the number
* 2. Multiply all the digits in odd positions (The first digit, the third
digit, etc) by 2.
* 3. If any one is greater than 9 subtract 9 from it.
* 4. Sum those numbers up
* 5. Add the even numbered digits (the second, fourth, etc) to the number
you got in the previous step
* 6. The check digit is the amount you need to add to that number to make
a multiple of 10.
* So if you got 68 in the previous step the check digit would be 2.
* You can calculate the digit in code using checkdigit = ((sum / 10 +
1) * 10 - sum) % 10
* -- http://www.darkcoding.net/index.php/.../luhn-formula/
*/
$sum = 0;
$pos = 0;
$rev = strrev($num);
$len = strlen($num);
if ($len % 2 == 0) $len += 1;
while ($pos < $len) {
$odd = $rev[$pos] * 2;
if ($odd 9) {
$odd -= 9;
}
$sum += $odd;
if ($pos != ($len - 2)) {
$sum += $rev[$pos +1];
}
$pos += 2;
}
return ((floor($sum/10) + 1) * 10 - $sum) % 10;
}
?>
Hi

Your example works well but produces two Notices in php.

Nel.
Output follows:
_______________________________________________

Notice: Uninitialized string offset: 6 in luhn.php on line 23

Notice: Uninitialized string offset: 7 in luhn.php on line 29
7001233
Mar 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.