📄 utilvalidate.java
字号:
int timeColon1 = time.indexOf(":");
int timeColon2 = time.lastIndexOf(":");
if (timeColon1 <= 0) return false;
hour = time.substring(0, timeColon1);
if (timeColon1 == timeColon2) {
minute = time.substring(timeColon1 + 1);
second = "0";
} else {
minute = time.substring(timeColon1 + 1, timeColon2);
second = time.substring(timeColon2 + 1);
}
return isTime(hour, minute, second);
}
/** Check to see if a card number is a valid ValueLink Gift Card
*
* @param stPassed a string representing a valuelink gift card
* @return true, if the number passed simple checks
*/
public static boolean isValueLinkCard(String stPassed) {
if (isEmpty(stPassed)) return defaultEmptyOK;
String st = stripCharsInBag(stPassed, creditCardDelimiters);
if (st.length() == 16 && (st.startsWith("7") || st.startsWith("6"))) {
return true;
}
return false;
}
/** Check to see if a card number is a supported Gift Card
*
* @param stPassed a string representing a gift card
* @return true, if the number passed simple checks
*/
public static boolean isGiftCard(String stPassed) {
return isValueLinkCard(stPassed);
}
public static int getLuhnSum(String stPassed) {
stPassed = stPassed.replaceAll("\\D", ""); // nuke any non-digit characters
int len = stPassed.length();
int sum = 0;
int mul = 1;
for (int i = len - 1; i >= 0; i--) {
int digit = Character.digit(stPassed.charAt(i), 10);
digit *= (mul == 1) ? mul++ : mul--;
sum += (digit >= 10) ? (digit % 10) + 1 : digit;
}
return sum;
}
public static int getLuhnCheckDigit(String stPassed) {
int sum = getLuhnSum(stPassed);
int mod = ((sum / 10 + 1) * 10 - sum) % 10;
return (10 - mod);
}
public static boolean sumIsMod10(int sum) {
return ((sum % 10) == 0);
}
public static String appendCheckDigit(String stPassed) {
String checkDigit = new Integer(getLuhnCheckDigit(stPassed)).toString();
return stPassed + checkDigit;
}
/** Checks credit card number with Luhn Mod-10 test
*
* @param stPassed a string representing a credit card number
* @return true, if the credit card number passes the Luhn Mod-10 test, false otherwise
*/
public static boolean isCreditCard(String stPassed) {
if (isEmpty(stPassed)) return defaultEmptyOK;
String st = stripCharsInBag(stPassed, creditCardDelimiters);
// encoding only works on cars with less the 19 digits
if (st.length() > 19) return false;
return sumIsMod10(getLuhnSum(st));
}
/** Checks to see if the cc number is a valid Visa number
*
* @param cc a string representing a credit card number; Sample number: 4111 1111 1111 1111(16 digits)
* @return true, if the credit card number is a valid VISA number, false otherwise
*/
public static boolean isVisa(String cc) {
if (((cc.length() == 16) || (cc.length() == 13)) && (cc.substring(0, 1).equals("4")))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid Master Card number
*
* @param cc a string representing a credit card number; Sample number: 5500 0000 0000 0004(16 digits)
* @return true, if the credit card number is a valid MasterCard number, false otherwise
*/
public static boolean isMasterCard(String cc) {
int firstdig = Integer.parseInt(cc.substring(0, 1));
int seconddig = Integer.parseInt(cc.substring(1, 2));
if ((cc.length() == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid American Express number
* @param cc - a string representing a credit card number; Sample number: 340000000000009(15 digits)
* @return true, if the credit card number is a valid American Express number, false otherwise
*/
public static boolean isAmericanExpress(String cc) {
int firstdig = Integer.parseInt(cc.substring(0, 1));
int seconddig = Integer.parseInt(cc.substring(1, 2));
if ((cc.length() == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid Diners Club number
* @param cc - a string representing a credit card number; Sample number: 30000000000004(14 digits)
* @return true, if the credit card number is a valid Diner's Club number, false otherwise
*/
public static boolean isDinersClub(String cc) {
int firstdig = Integer.parseInt(cc.substring(0, 1));
int seconddig = Integer.parseInt(cc.substring(1, 2));
if ((cc.length() == 14) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid Carte Blanche number
* @param cc - a string representing a credit card number; Sample number: 30000000000004(14 digits)
* @return true, if the credit card number is a valid Carte Blanche number, false otherwise
*/
public static boolean isCarteBlanche(String cc) {
return isDinersClub(cc);
}
/** Checks to see if the cc number is a valid Discover number
* @param cc - a string representing a credit card number; Sample number: 6011000000000004(16 digits)
* @return true, if the credit card number is a valid Discover card number, false otherwise
*/
public static boolean isDiscover(String cc) {
String first4digs = cc.substring(0, 4);
if ((cc.length() == 16) && (first4digs.equals("6011")))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid EnRoute number
* @param cc - a string representing a credit card number; Sample number: 201400000000009(15 digits)
* @return true, if the credit card number is a valid enRoute card number, false, otherwise
*/
public static boolean isEnRoute(String cc) {
String first4digs = cc.substring(0, 4);
if ((cc.length() == 15) && (first4digs.equals("2014") || first4digs.equals("2149")))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid JCB number
* @param cc - a string representing a credit card number; Sample number: 3088000000000009(16 digits)
* @return true, if the credit card number is a valid JCB card number, false otherwise
*/
public static boolean isJCB(String cc) {
String first4digs = cc.substring(0, 4);
if ((cc.length() == 16) &&
(first4digs.equals("3088") ||
first4digs.equals("3096") ||
first4digs.equals("3112") ||
first4digs.equals("3158") ||
first4digs.equals("3337") ||
first4digs.equals("3528")))
return isCreditCard(cc);
return false;
}
/** Checks to see if the cc number is a valid number for any accepted credit card
* @param ccPassed - a string representing a credit card number
* @return true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
*/
public static boolean isAnyCard(String ccPassed) {
if (isEmpty(ccPassed)) return defaultEmptyOK;
String cc = stripCharsInBag(ccPassed, creditCardDelimiters);
if (!isCreditCard(cc)) return false;
if (isMasterCard(cc) || isVisa(cc) || isAmericanExpress(cc) || isDinersClub(cc) ||
isDiscover(cc) || isEnRoute(cc) || isJCB(cc))
return true;
return false;
}
/** Checks to see if the cc number is a valid number for any accepted credit card, and return the name of that type
* @param ccPassed - a string representing a credit card number
* @return true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
*/
public static String getCardType(String ccPassed) {
if (isEmpty(ccPassed)) return "Unknown";
String cc = stripCharsInBag(ccPassed, creditCardDelimiters);
if (!isCreditCard(cc)) return "Unknown";
if (isMasterCard(cc)) return "MasterCard";
if (isVisa(cc)) return "Visa";
if (isAmericanExpress(cc)) return "AmericanExpress";
if (isDinersClub(cc)) return "DinersClub";
if (isDiscover(cc)) return "Discover";
if (isEnRoute(cc)) return "EnRoute";
if (isJCB(cc)) return "JCB";
return "Unknown";
}
/** Checks to see if the cc number is a valid number for the specified type
* @param cardType - a string representing the credit card type
* @param cardNumberPassed - a string representing a credit card number
* @return true, if the credit card number is valid for the particular credit card type given in "cardType", false otherwise
*/
public static boolean isCardMatch(String cardType, String cardNumberPassed) {
if (isEmpty(cardType)) return defaultEmptyOK;
if (isEmpty(cardNumberPassed)) return defaultEmptyOK;
String cardNumber = stripCharsInBag(cardNumberPassed, creditCardDelimiters);
if ((cardType.equalsIgnoreCase("VISA")) && (isVisa(cardNumber))) return true;
if ((cardType.equalsIgnoreCase("MASTERCARD")) && (isMasterCard(cardNumber))) return true;
if (((cardType.equalsIgnoreCase("AMERICANEXPRESS")) || (cardType.equalsIgnoreCase("AMEX"))) && (isAmericanExpress(cardNumber))) return true;
if ((cardType.equalsIgnoreCase("DISCOVER")) && (isDiscover(cardNumber))) return true;
if ((cardType.equalsIgnoreCase("JCB")) && (isJCB(cardNumber))) return true;
if (((cardType.equalsIgnoreCase("DINERSCLUB")) || (cardType.equalsIgnoreCase("DINERS"))) && (isDinersClub(cardNumber))) return true;
if ((cardType.equalsIgnoreCase("CARTEBLANCHE")) && (isCarteBlanche(cardNumber))) return true;
if ((cardType.equalsIgnoreCase("ENROUTE")) && (isEnRoute(cardNumber))) return true;
return false;
}
/** isNotPoBox returns true if address argument does not contain anything that looks like a a PO Box. */
public static boolean isNotPoBox(String s) {
if (isEmpty(s)) return defaultEmptyOK;
// strings to check from Greg's program
// "P.O. B"
// "P.o.B"
// "P.O B"
// "PO. B"
// "P O B"
// "PO B"
// "P.0. B"
// "P0 B"
String sl = s.toLowerCase();
if (sl.indexOf("p.o. b") != -1) return false;
if (sl.indexOf("p.o.b") != -1) return false;
if (sl.indexOf("p.o b") != -1) return false;
if (sl.indexOf("p o b") != -1) return false;
if (sl.indexOf("po b") != -1) return false;
if (sl.indexOf("pobox") != -1) return false;
if (sl.indexOf("po#") != -1) return false;
if (sl.indexOf("po #") != -1) return false;
// now with 0's for them sneaky folks
if (sl.indexOf("p.0. b") != -1) return false;
if (sl.indexOf("p.0.b") != -1) return false;
if (sl.indexOf("p.0 b") != -1) return false;
if (sl.indexOf("p 0 b") != -1) return false;
if (sl.indexOf("p0 b") != -1) return false;
if (sl.indexOf("p0box") != -1) return false;
if (sl.indexOf("p0#") != -1) return false;
if (sl.indexOf("p0 #") != -1) return false;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -