the trailboss abuses his CodeRanch power for his other stuff (power corrupts. absolute power corrupts absolutely is kinda neat!)
permaculture light bulbs permaculture electric heat permaculture cast iron permaculture wood burning stove permaculture solar food dehydrators
The moose likes Meaningless Drivel and the fly likes Credit card validation algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Meaningless Drivel
Bookmark "Credit card validation algorithm" Watch "Credit card validation algorithm" New topic
Author

Credit card validation algorithm

krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
Hey,
I need an algorithm to validate master, visa and amex cards. If u have an algorithm, please do post that at the earliest. will be very thankful to you.
- chandrasekaran
------------------
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
Isn't that illegal?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Krish, why do u ask about validation algo on Credit Cards?
If you're workin for a company dealing with this stuff, you should have easy access to these algorithms.
Kumat Karazu
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
http://www.google.com/search?q=Credit+card+validation+algorithm+
krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
Illegal ??conrad , I dont believe so. well i just wanna write my app that validates the user credit card number and no impure intensions and nothing against cyber laws. i am not aware if there is some law aginst this !
enlight me if there is !!
and Mr ( or miss ??? ) CC CC...That was the first thing any one seeking somethin will do ( i mean going to google !!) :-) and am no exception for this !! Thanx any way !!
I am just curious abt how these sites vaidates the payment ! or things like that !
More information in this area are welcome !!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
my / miss / mrs / mast chandu.. if you were wise enuf to do a google search, did you check out the results also ? browsing the 4th result http://www.infohiway.com/javascript/ccwizard.htm and further homework will lead to http://www.infohiway.com/javascript/ccwizard.txt and http://developer.netscape.com/library/examples/javascript/formval/FormChek.js which should help you get an understanding. 'course these all are client side script but you should be able to adapt them to the server side if you need.
You are in the wrong place if you think the visitors of this forum are going to take you on their lap and spoon-feed you like your mother did when you were 1.
Mama.. Mama.. look what CC CC said to me
Sahir Shibley
Ranch Hand

Joined: Apr 08, 2000
Posts: 275
In view of the recent allegations against me of being a pro self reliance right winger. I have decided to do a volte face and turn into Mr.Helpful. So I sat down and coded the whole thing for you in Java. This uses a checksum algorithm here. You can remove the main method and use this class as it is with a few minor modifications to make it a little more robust.
<pre>
public class CreditCard{
private String ccNbr;
private Integer digit;
public CreditCard(String cardNbr){
ccNbr= stripChars(cardNbr);
}
private String stripChars(String cardNbr){
/* Add the body of the function
to strip non digit characters
from the nbr such as space , hyphen , tab etc
as users may enter spaces to match
the spacing in the card
and return the stripped string from this function
*/
return cardNbr;
}
public boolean validate(){
int charVal = 0;int ckDigit = 0;
boolean flipflop = true; int checkSum = 0;
int tempVal = 0;
if(ccNbr.length() < 1) return false;<br /> if(ccNbr.length() > 16) return false;
digit = new Integer(ccNbr.substring(ccNbr.length()-1 , ccNbr.length()));
ckDigit = digit.intValue();
for(int i= ccNbr.length()-1; i > 0 ; i--)
{
charVal = ccNbr.charAt(i);
if(charVal > 57 | | charVal < 48) return false;<br /> digit = new Integer(ccNbr.substring(i-1 , i));<br /> charVal = digit.intValue();<br /> if(flipflop) {<br /> flipflop = false;<br /> tempVal = charVal * 2;<br /> if(tempVal > 9) tempVal = tempVal - 9;
checkSum = checkSum + tempVal;
}
else {
flipflop = true;
checkSum = checkSum + (charVal);
}
}
if(checkSum < 1) return false;
checkSum = checkSum + ckDigit;
checkSum = checkSum % 10;
if(checkSum == 0)
return true;
else
return false;
}
public static void main(String[] args) {
CreditCard cc = new CreditCard("0000000000000000");
boolean test = cc.validate();
System.out.println(test);
}
}
</pre>

Cheers
Sahir

[This message has been edited by Sahir Shibley (edited July 09, 2001).]
krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
Originally posted by CC CC:
my / miss / mrs / mast chandu.. if you were wise enuf to do a google search, did you check out the results also ? browsing the 4th result http://www.infohiway.com/javascript/ccwizard.htm and further homework will lead to http://www.infohiway.com/javascript/ccwizard.txt and http://developer.netscape.com/library/examples/javascript/formval/FormChek.js which should help you get an understanding. 'course these all are client side script but you should be able to adapt them to the server side if you need.
You are in the wrong place if you think the visitors of this forum are going to take you on their lap and spoon-feed you like your mother did when you were 1.
Mama.. Mama.. look what CC CC said to me

hey CC
Thanx for the detailed link and i in no chance expect someone to spoon feed me :-) Well, i believe this implementation of mod 10 algorithm is already available with me and i thought i will be getting some alternative algorithm for validating the cards :-)
Thanx again for the efforts !
Sahir Shibley
Ranch Hand

Joined: Apr 08, 2000
Posts: 275
I thought a little explanation of the checksum algorithm will be helpful. Most credit card numbers are 16 digits a few like AX have less (this class works with all of them). The last digit in the card number is the check digit. Take out the last digit and start doing the check sum on the remaining digits from right to left. You alternately multiply the digits by 2 and 1. If the result on multiplication by 2 is greater than 9 then substract 9 from the result. Add the resulting digits together then add the check digit and finally do a modulo 10 check. If the modulo result is positive (ie. result is zero) then the credit card number has been entered correctly. This is the method used by the credit card company's software that generates the card number to finally verify the number, so you can more or less rely on it.
<pre>
As an example we shall use a five digit card number : 42994
The last digit 4 is the check digit.
If you remove that you are left with 4299
now you go
4*1 2*2 9*1 9 * 2
4 4 9 9 (ie 18-9)
and 4 + 4 + 9 + 9 = 26
26 + 4 = 30
30 mod 10 = 0
If there were such a thing as five digit credit card numbers this would have been a valid credit card number.
</pre>

Cheers
Sahir

[This message has been edited by Sahir Shibley (edited July 09, 2001).]
krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
hey sahir,
Thanx very much for the detailed explanations. but as said here how strict r the cyber laws in protecting the virtual money. Any updates in cyber laws ?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

If you just want the functionality you can get credit card validation software for free. here are a few links. http://www.psigate.com/freecom.html http://www.ecommerce-info.lv/credit_card_av.html
or maybe http://www.xequte.com/cardchck.html
I have no personal knowledge of them though.


SCJP
Visit my download page
Pranav Jaidka
Ranch Hand

Joined: Sep 14, 2000
Posts: 264
I need someones credit card number to verify an algorithm i developed ...could someone please send it to me ....
krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
Y donyya back trace the algorithm and get some numbers ! And do let us also know that ;-)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Credit card validation algorithm
 
Similar Threads
optimal way to grab closest sum
maze program help
Problem with sorting an integer array
Problem with Cryptography!!!
WA #1.....word association