📄 utilvalidate.java
字号:
return false;
}
/** isTime returns true if string argument time forms a valid time. */
public static boolean isTime(String time) {
if (isEmpty(time)) return defaultEmptyOK;
String hour;
String minute;
String second;
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);
}
/** 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);
int sum = 0;
int mul = 1;
int l = st.length();
// Encoding only works on cards with less than 19 digits
if (l > 19) return (false);
for (int i = 0; i < l; i++) {
String digit = st.substring(l - i - 1, l - i);
int tproduct = 0;
try {
tproduct = Integer.parseInt(digit, 10) * mul;
} catch (Exception e) {
Debug.logWarning(e.getMessage());
return false;
}
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul--;
}
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
// the next multiple of 10.
// document.writeln("<BR>Sum = ",sum,"<BR>");
// alert("Sum = " + sum);
if ((sum % 10) == 0)
return true;
else
return false;
}
/** 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 + -