这是一个非常有用的工具类用于验证credit card, 是apache alidation的工具类。
http://www.apache.org/licenses/LICENSE-2.0
package org.apache.commons.validator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.validator.util.Flags;
/**
* <p>Perform credit card validations.</p>
* <p>
* By default, all supported card types are allowed. You can specify which
* cards should pass validation by configuring the validation options. For
* example,<br/><code>CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.AMEX + CreditCardValidator.VISA);</code>
* configures the validator to only pass American Express and Visa cards.
* If a card type is not directly supported by this class, you can implement
* the CreditCardType interface and pass an instance into the
* <code>addAllowedCardType</code> method.
* </p>
* For a similar implementation in Perl, reference Sean M. Burke's
* <a href="http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html">script</a>.
* More information is also available
* <a href="http://www.merriampark.com/anatomycc.htm">here</a>.
*
* @since Validator 1.1
*/
public class CreditCardValidator {
/**
* Option specifying that no cards are allowed. This is useful if
* you want only custom card types to validate so you turn off the
* default cards with this option.
* <br/>
* <pre>
* CreditCardValidator v = new CreditCardValidator(CreditCardValidator.NONE);
* v.addAllowedCardType(customType);
* v.isValid(aCardNumber);
* </pre>
* @since Validator 1.1.2
*/
public static final int NONE = 0;
/**
* Option specifying that American Express cards are allowed.
*/
public static final int AMEX = 1 << 0;
/**
* Option specifying that Visa cards are allowed.
*/
public static final int VISA = 1 << 1;
/**
* Option specifying that Mastercard cards are allowed.
*/
public static final int MASTERCARD = 1 << 2;
/**
* Option specifying that Discover cards are allowed.
*/
public static final int DISCOVER = 1 << 3;
/**
* The CreditCardTypes that are allowed to pass validation.
*/
private Collection cardTypes = new ArrayList();
/**
* Create a new CreditCardValidator with default options.
*/
public CreditCardValidator() {
this(AMEX + VISA + MASTERCARD + DISCOVER);
}
/**
* Create a new CreditCardValidator with the specified options.
* @param options Pass in
* CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that
* those are the only valid card types.
*/
public CreditCardValidator(int options) {
super();
Flags f = new Flags(options);
if (f.isOn(VISA)) {
this.cardTypes.add(new Visa());
}
if (f.isOn(AMEX)) {
this.cardTypes.add(new Amex());
}
if (f.isOn(MASTERCARD)) {
this.cardTypes.add(new Mastercard());
}
if (f.isOn(DISCOVER)) {
this.cardTypes.add(new Discover());
}
}
/**
* Checks if the field is a valid credit card number.
* @param card The card number to validate.
*/
public boolean isValid(String card) {
if ((card == null) || (card.length() < 13) || (card.length() > 19)) {
return false;
}
if (!this.luhnCheck(card)) {
return false;
}
Iterator types = this.cardTypes.iterator();
while (types.hasNext()) {
CreditCardType type = (CreditCardType) types.next();
if (type.matches(card)) {
return true;
}
}
return false;
}
/**
* Add an allowed CreditCardType that participates in the card
* validation algorithm.
* @param type The type that is now allowed to pass validation.
* @since Validator 1.1.2
*/
public void addAllowedCardType(CreditCardType type){
this.cardTypes.add(type);
}
/**
* Checks for a valid credit card number.
* @param cardNumber Credit Card Number.
*/
protected boolean luhnCheck(String cardNumber) {
// number must be validated as 0..9 numeric first!!
int digits = cardNumber.length();
int oddOrEven = digits & 1;
long sum = 0;
for (int count = 0; count < digits; count++) {
int digit = 0;
try {
digit = Integer.parseInt(cardNumber.charAt(count) + "");
} catch(NumberFormatException e) {
return false;
}
if (((count & 1) ^ oddOrEven) == 0) { // not
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
}
return (sum == 0) ? false : (sum % 10 == 0);
}
/**
* Checks for a valid credit card number.
* @param card Credit Card Number.
* @deprecated This will be removed in a future release.
*/
protected boolean isValidPrefix(String card) {
if (card.length() < 13) {
return false;
}
return new Visa().matches(card)
|| new Amex().matches(card)
|| new Mastercard().matches(card)
|| new Discover().matches(card);
}
/**
* CreditCardType implementations define how validation is performed
* for one type/brand of credit card.
* @since Validator 1.1.2
*/
public interface CreditCardType {
/**
* Returns true if the card number matches this type of credit
* card. Note that this method is <strong>not</strong> responsible
* for analyzing the general form of the card number because
* <code>CreditCardValidator</code> performs those checks before
* calling this method. It is generally only required to valid the
* length and prefix of the number to determine if it's the correct
* type.
* @param card The card number, never null.
* @return true if the number matches.
*/
boolean matches(String card);
}
private class Visa implements CreditCardType {
private static final String PREFIX = "4";
public boolean matches(String card) {
return (
card.substring(0, 1).equals(PREFIX)
&& (card.length() == 13 || card.length() == 16));
}
}
private class Amex implements CreditCardType {
private static final String PREFIX = "34,37,";
public boolean matches(String card) {
String prefix2 = card.substring(0, 2) + ",";
return ((PREFIX.indexOf(prefix2) != -1) && (card.length() == 15));
}
}
private class Discover implements CreditCardType {
private static final String PREFIX = "6011";
public boolean matches(String card) {
return (card.substring(0, 4).equals(PREFIX) && (card.length() == 16));
}
}
private class Mastercard implements CreditCardType {
private static final String PREFIX = "51,52,53,54,55,";
public boolean matches(String card) {
String prefix2 = card.substring(0, 2) + ",";
return ((PREFIX.indexOf(prefix2) != -1) && (card.length() == 16));
}
}
}
发表评论
-
Classloader
2011-11-22 14:09 718The class loader: BootStrap c ... -
JUnit Knowledges
2011-10-25 23:09 710mock objects: http://bigwhite. ... -
Java跨平台程序设计规则
2011-09-20 12:38 921不是说所有的Java程序都具有Java跨平台的特性,事实上,相 ... -
Servlet - life cycle and listener events
2011-08-25 15:56 788import database.BookDBAO; ... -
Common Gateway Interface
2011-08-25 15:20 1363A web server that supports ... -
什么时候使用属性或者元素(Designing XML document)
2011-08-19 11:34 979Properties 是平面结构,想要表达结构复杂的信息有一定 ... -
Java - Generic
2011-07-19 12:35 687Generics <!--[if !suppor ... -
Java Exception Study
2011-07-13 16:37 815Java Exception <!--[if ! ... -
面向对象的设计的四个主要原则
2011-07-11 14:22 13131. ISP (Interface Segregation ... -
Design Pattern
2011-07-11 11:52 625Mediator 一、引子 中介在现实 生活中并 ... -
Java Generic, Association, Composition, Aggregate
2011-07-05 16:20 1125UML中依赖泛化关联实现聚合组合的Java实现 ... -
ETL
2011-06-30 11:36 827ETL 对于数据仓库以及ETL的知识,我基本上是个 ... -
Java Collection Frameworks 学习
2011-06-30 11:17 665Java 集合总结 <!--[if !su ... -
Java Clone
2011-06-29 16:07 774Clone interface: 如果一个实现了Cl ... -
实例化抽象类时实现抽象类里的抽象方法
2010-04-16 09:18 980EccTrxCmd logWSCall = new EccTr ... -
Java reflect
2010-03-09 16:21 786【转】Java反射经典实例 Java Reflection C ...
相关推荐
11贷款预测-creditcard.csv 数据集.zip11贷款预测-creditcard.csv 数据集.zip11贷款预测-creditcard.csv 数据集.zip11贷款预测-creditcard.csv 数据集.zip11贷款预测-creditcard.csv 数据集.zip
creditcard csv - 训练集 信用卡数据 信用卡训练数据, 有兴趣的童鞋们可以下载
creditcard训练集经处理,
信用卡欺诈检测数据
内含竞赛数据集creditcard.csv,简单处理imbalaced问题,采用逻辑回归算法,用度量标准进行评价。 ·Jupyter notebook
交易数据异常检测--creditcard.zip
此文件是Java中的credit card class, 其中包含了 卡号鉴别器。
cret edit call me call met
可以验证各种信用卡的有效性,如Visa,MasterCard……
creditcard.js, 在JavaScript中,一个简单的信用卡验证库 creditcard.js JavaScript中一个简单的信用卡验证库。安装你可以下载zip文件,或者使用NPM和 Bower 。 NPM$ npm install --save creditcard.js
一个功能强大的CreditCard验证码控件源码及例子CreditCardValidator_v3_source_Example。
Credit Card beta1.0.20100115信用卡基础知识 银行系统开发,银行业务知识整理完成 银行业务知识汇编全稿
Credit card fraud 数据集包含了 2013 年九月欧洲的信用卡持有者的交易记录。交易次数共有 284807 次,其中的 492 次为诈骗交易。该数据集多用于金融领域
data science about creditcard
信用卡卡号识别,基于模板匹配的方式。
credit card fraud detection
In the present, I focus on security protocols in the electronic payment, particularly electronic security protocol set developed by a group of large credit card companies like Visa, MasterCard and ...
SAS 信用卡 UCI数据 数据集信息 ... 内容 ...LIMIT_BAL:以新台币计的给定信用额度(包括个人和家庭/辅助信用额) ...教育程度:(1 =研究生院,2 =大学,3 =高中,4 =其他,5 =未知,6 =未知) ...BILL_AMT2:2005年