1 24 package org.ofbiz.base.util; 25 26 import java.util.Calendar ; 27 import java.util.Collection ; 28 29 67 public class UtilValidate { 68 69 public static final String module = UtilValidate.class.getName(); 70 71 72 public static final boolean defaultEmptyOK = true; 73 74 75 public static final String digits = "0123456789"; 76 77 78 public static final String lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"; 79 80 81 public static final String uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 82 83 84 public static final String letters = lowercaseLetters + uppercaseLetters; 85 86 87 public static final String whitespace = " \t\n\r"; 88 89 90 public static final String decimalPointDelimiter = "."; 91 92 93 public static final String phoneNumberDelimiters = "()- "; 94 95 96 public static final String validUSPhoneChars = digits + phoneNumberDelimiters; 97 98 99 public static final String validWorldPhoneChars = digits + phoneNumberDelimiters + "+"; 100 101 102 public static final String SSNDelimiters = "- "; 103 104 105 public static final String validSSNChars = digits + SSNDelimiters; 106 107 108 public static final int digitsInSocialSecurityNumber = 9; 109 110 111 public static final int digitsInUSPhoneNumber = 10; 112 public static final int digitsInUSPhoneAreaCode = 3; 113 public static final int digitsInUSPhoneMainNumber = 7; 114 115 116 public static final String ZipCodeDelimiters = "-"; 117 118 119 public static final String ZipCodeDelimeter = "-"; 120 121 122 public static final String validZipCodeChars = digits + ZipCodeDelimiters; 123 124 125 public static final int digitsInZipCode1 = 5; 126 127 128 public static final int digitsInZipCode2 = 9; 129 130 131 public static final String creditCardDelimiters = " -"; 132 133 public static final String isNotEmptyMsg = "This field cannot be empty, please enter a value."; 134 public static final String isStateCodeMsg = "The State Code must be a valid two character U.S. state abbreviation(like CA for California)."; 135 public static final String isContiguousStateCodeMsg = "The State Code must be a valid two character U.S. state abbreviation for one of the 48 contiguous United States (like CA for California)."; 136 public static final String isZipCodeMsg = "The ZIP Code must be a 5 or 9 digit U.S. ZIP Code(like 94043)."; 137 public static final String isUSPhoneMsg = "The US Phone must be a 10 digit U.S. phone number(like 415-555-1212)."; 138 public static final String isUSPhoneAreaCodeMsg = "The Phone Number Area Code must be 3 digits."; 139 public static final String isUSPhoneMainNumberMsg = "The Phone Number must be 7 digits."; 140 public static final String isContiguousZipCodeMsg = "Zip Code is not a valid Zip Code for one of the 48 contiguous United States ."; 141 public static final String isInternationalPhoneNumberMsg = "The World Phone must be a valid international phone number."; 142 public static final String isSSNMsg = "The SSN must be a 9 digit U.S. social security number(like 123-45-6789)."; 143 public static final String isEmailMsg = "The Email must be a valid email address(like john@email.com). Please re-enter it now."; 144 public static final String isAnyCardMsg = "The credit card number is not a valid card number."; 145 public static final String isCreditCardPrefixMsg = " is not a valid "; 146 public static final String isCreditCardSuffixMsg = " credit card number."; 147 public static final String isDayMsg = "The Day must be a day number between 1 and 31. "; 148 public static final String isMonthMsg = "The Month must be a month number between 1 and 12. "; 149 public static final String isYearMsg = "The Year must be a 2 or 4 digit year number. "; 150 public static final String isDatePrefixMsg = "The Day, Month, and Year for "; 151 public static final String isDateSuffixMsg = " do not form a valid date. Please reenter them now."; 152 public static final String isHourMsg = "The Hour must be a number between 0 and 23."; 153 public static final String isMinuteMsg = "The Hour must be a number between 0 and 59."; 154 public static final String isSecondMsg = "The Hour must be a number between 0 and 59."; 155 public static final String isTimeMsg = "The Time must be a valid time formed like: HH:MM or HH:MM:SS."; 156 public static final String isDateMsg = "The Date must be a valid date formed like: MM/YY, MM/YYYY, MM/DD/YY, or MM/DD/YYYY."; 157 public static final String isDateAfterToday = "The Date must be a valid date after today, and formed like: MM/YY, MM/YYYY, MM/DD/YY, or MM/DD/YYYY."; 158 public static final String isIntegerMsg = "The Number must be a valid unsigned whole decimal number."; 159 public static final String isSignedIntegerMsg = "The Number must be a valid signed whole decimal number."; 160 public static final String isLongMsg = "The Number must be a valid unsigned whole decimal number."; 161 public static final String isSignedLongMsg = "The Number must be a valid signed whole decimal number."; 162 public static final String isFloatMsg = "The Number must be a valid unsigned decimal number."; 163 public static final String isSignedFloatMsg = "The Number must be a valid signed decimal number."; 164 public static final String isSignedDoubleMsg = "The Number must be a valid signed decimal number."; 165 166 168 public static final int[] daysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 169 170 171 public static final String USStateCodeDelimiter = "|"; 172 173 175 public static final String USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP"; 176 177 178 public static final String ContiguousUSStateCodes = "AL|AZ|AR|CA|CO|CT|DE|DC|FL|GA|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY"; 179 180 public static boolean areEqual(Object obj, Object obj2) { 181 if (obj == null) { 182 return obj2 == null; 183 } else { 184 return obj.equals(obj2); 185 } 186 } 187 188 189 public static boolean isEmpty(Object o) { 190 return ObjectType.isEmpty(o); 191 } 192 193 194 public static boolean isEmpty(String s) { 195 return ((s == null) || (s.length() == 0)); 196 } 197 198 199 public static boolean isEmpty(Collection c) { 200 return ((c == null) || (c.size() == 0)); 201 } 202 203 204 public static boolean isNotEmpty(String s) { 205 return ((s != null) && (s.length() > 0)); 206 } 207 208 209 public static boolean isNotEmpty(Collection c) { 210 return ((c != null) && (c.size() > 0)); 211 } 212 213 public static boolean isString(Object obj) { 214 return ((obj != null) && (obj instanceof java.lang.String )); 215 } 216 217 218 public static boolean isWhitespace(String s) { 219 if (isEmpty(s)) return true; 221 222 for (int i = 0; i < s.length(); i++) { 226 char c = s.charAt(i); 228 229 if (whitespace.indexOf(c) == -1) return false; 230 } 231 return true; 233 } 234 235 236 public static String stripCharsInBag(String s, String bag) { 237 int i; 238 String returnString = ""; 239 240 for (i = 0; i < s.length(); i++) { 243 char c = s.charAt(i); 244 245 if (bag.indexOf(c) == -1) returnString += c; 246 } 247 return returnString; 248 } 249 250 251 public static String stripCharsNotInBag(String s, String bag) { 252 int i; 253 String returnString = ""; 254 255 for (i = 0; i < s.length(); i++) { 258 char c = s.charAt(i); 259 260 if (bag.indexOf(c) != -1) returnString += c; 261 } 262 return returnString; 263 } 264 265 267 public static String stripWhitespace(String s) { 268 return stripCharsInBag(s, whitespace); 269 } 270 271 272 public static boolean charInString(char c, String s) { 273 return (s.indexOf(c) != -1); 274 } 279 280 282 public static String stripInitialWhitespace(String s) { 283 int i = 0; 284 285 while ((i < s.length()) && charInString(s.charAt(i), whitespace)) i++; 286 return s.substring(i); 287 } 289 290 295 public static boolean isLetter(char c) { 296 return Character.isLetter(c); 297 } 298 299 300 public static boolean isDigit(char c) { 301 return Character.isDigit(c); 302 } 303 304 305 public static boolean isLetterOrDigit(char c) { 306 return Character.isLetterOrDigit(c); 307 } 308 309 314 public static boolean isInteger(String s) { 315 if (isEmpty(s)) return defaultEmptyOK; 316 317 for (int i = 0; i < s.length(); i++) { 321 char c = s.charAt(i); 323 324 if (!isDigit(c)) return false; 325 } 326 327 return true; 329 } 330 331 336 public static boolean isSignedInteger(String s) { 337 if (isEmpty(s)) return defaultEmptyOK; 338 try { 339 Integer.parseInt(s); 340 return true; 341 } catch (Exception e) { 342 return false; 343 } 344 345 348 350 } 354 355 360 public static boolean isSignedLong(String s) { 361 if (isEmpty(s)) return defaultEmptyOK; 362 try { 363 Long.parseLong(s); 364 return true; 365 } catch (Exception e) { 366 return false; 367 } 368 } 369 370 371 public static boolean isPositiveInteger(String s) { 372 if (isEmpty(s)) return defaultEmptyOK; 373 374 try { 375 long temp = Long.parseLong(s); 376 377 if (temp > 0) return true; 378 return false; 379 } catch (Exception e) { 380 return false; 381 } 382 383 } 386 387 388 public static boolean isNonnegativeInteger(String s) { 389 if (isEmpty(s)) return defaultEmptyOK; 390 391 try { 392 int temp = Integer.parseInt(s); 393 394 if (temp >= 0) return true; 395 return false; 396 } catch (Exception e) { 397 return false; 398 } 399 400 } 403 404 405 public static boolean isNegativeInteger(String s) { 406 if (isEmpty(s)) return defaultEmptyOK; 407 408 try { 409 int temp = Integer.parseInt(s); 410 411 if (temp < 0) return true; 412 return false; 413 } catch (Exception e) { 414 return false; 415 } 416 417 } 420 421 422 public static boolean isNonpositiveInteger(String s) { 423 if (isEmpty(s)) return defaultEmptyOK; 424 425 try { 426 int temp = Integer.parseInt(s); 427 428 if (temp <= 0) return true; 429 return false; 430 } catch (Exception e) { 431 return false; 432 } 433 434 } 437 438 446 public static boolean isFloat(String s) { 447 if (isEmpty(s)) return defaultEmptyOK; 448 449 boolean seenDecimalPoint = false; 450 451 if (s.startsWith(decimalPointDelimiter)) return false; 452 453 for (int i = 0; i < s.length(); i++) { 457 char c = s.charAt(i); 459 460 if (c == decimalPointDelimiter.charAt(0)) { 461 if (!seenDecimalPoint) { 462 seenDecimalPoint = true; 463 } else { 464 return false; 465 } 466 } else { 467 if (!isDigit(c)) return false; 468 } 469 } 470 return true; 472 } 473 474 476 public static boolean isFloat(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) { 477 if (isEmpty(s)) return defaultEmptyOK; 478 479 try { 480 float temp = Float.parseFloat(s); 481 if (!allowNegative && temp < 0) return false; 482 if (!allowPositive && temp > 0) return false; 483 String floatString = Float.toString(temp); 484 int decimalPoint = floatString.indexOf("."); 485 if (decimalPoint == -1) { 486 if (minDecimal > 0) return false; 487 return true; 488 } 489 int numDecimals = floatString.length() - decimalPoint; 491 if (minDecimal >= 0 && numDecimals < minDecimal) return false; 492 if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; 493 return true; 494 } catch (Exception e) { 495 return false; 496 } 497 } 498 499 501 public static boolean isDouble(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) { 502 if (isEmpty(s)) return defaultEmptyOK; 503 504 try { 505 double temp = Double.parseDouble(s); 506 if (!allowNegative && temp < 0) return false; 507 if (!allowPositive && temp > 0) return false; 508 String doubleString = Double.toString(temp); 509 int decimalPoint = doubleString.indexOf("."); 510 if (decimalPoint == -1) { 511 if (minDecimal > 0) return false; 512 return true; 513 } 514 int numDecimals = doubleString.length() - decimalPoint; 516 if (minDecimal >= 0 && numDecimals < minDecimal) return false; 517 if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; 518 return true; 519 } catch (Exception e) { 520 return false; 521 } 522 } 523 524 531 public static boolean isSignedFloat(String s) { 532 if (isEmpty(s)) return defaultEmptyOK; 533 try { 534 Float.parseFloat(s); 535 return true; 536 } catch (Exception e) { 537 return false; 538 } 539 540 } 547 548 555 public static boolean isSignedDouble(String s) { 556 if (isEmpty(s)) return defaultEmptyOK; 557 try { 558 Double.parseDouble(s); 559 return true; 560 } catch (Exception e) { 561 return false; 562 } 563 } 564 565 570 public static boolean isAlphabetic(String s) { 571 if (isEmpty(s)) return defaultEmptyOK; 572 573 for (int i = 0; i < s.length(); i++) { 577 char c = s.charAt(i); 579 580 if (!isLetter(c)) 581 return false; 582 } 583 584 return true; 586 } 587 588 594 public static boolean isAlphanumeric(String s) { 595 if (isEmpty(s)) return defaultEmptyOK; 596 597 for (int i = 0; i < s.length(); i++) { 601 char c = s.charAt(i); 603 604 if (!isLetterOrDigit(c)) return false; 605 } 606 607 return true; 609 } 610 611 612 613 614 public static boolean isSSN(String s) { 615 if (isEmpty(s)) return defaultEmptyOK; 616 617 String normalizedSSN = stripCharsInBag(s, SSNDelimiters); 618 619 return (isInteger(normalizedSSN) && normalizedSSN.length() == digitsInSocialSecurityNumber); 620 } 621 622 623 public static boolean isUSPhoneNumber(String s) { 624 if (isEmpty(s)) return defaultEmptyOK; 625 String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); 626 627 return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneNumber); 628 } 629 630 631 public static boolean isUSPhoneAreaCode(String s) { 632 if (isEmpty(s)) return defaultEmptyOK; 633 String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); 634 635 return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneAreaCode); 636 } 637 638 639 public static boolean isUSPhoneMainNumber(String s) { 640 if (isEmpty(s)) return defaultEmptyOK; 641 String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); 642 643 return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneMainNumber); 644 } 645 646 650 public static boolean isInternationalPhoneNumber(String s) { 651 if (isEmpty(s)) return defaultEmptyOK; 652 653 String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters); 654 655 return isPositiveInteger(normalizedPhone); 656 } 657 658 659 public static boolean isZipCode(String s) { 660 if (isEmpty(s)) return defaultEmptyOK; 661 662 String normalizedZip = stripCharsInBag(s, ZipCodeDelimiters); 663 664 return (isInteger(normalizedZip) && ((normalizedZip.length() == digitsInZipCode1) || (normalizedZip.length() == digitsInZipCode2))); 665 } 666 667 668 public static boolean isContiguousZipCode(String s) { 669 boolean retval = false; 670 if (isZipCode(s)) { 671 if (isEmpty(s)) retval = defaultEmptyOK; 672 else { 673 String normalizedZip = s.substring(0,5); 674 int iZip = Integer.parseInt(normalizedZip); 675 if ((iZip >= 96701 && iZip <= 96898) || (iZip >= 99501 && iZip <= 99950)) retval = false; 676 else retval = true; 677 } 678 } 679 return retval; 680 } 681 682 683 public static boolean isStateCode(String s) { 684 if (isEmpty(s)) return defaultEmptyOK; 685 return ((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1)); 686 } 687 688 689 public static boolean isContiguousStateCode(String s) { 690 if (isEmpty(s)) return defaultEmptyOK; 691 return ((ContiguousUSStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1)); 692 } 693 694 public static boolean isEmail(String s) { 695 return isEmail(s, false); 696 } 697 698 704 public static boolean isEmail(String s, boolean requireDot) { 705 706 708 if (isEmpty(s)) return defaultEmptyOK; 709 710 if (isWhitespace(s)) return false; 712 713 int atSymbolIndex = s.indexOf('@'); 714 715 if (atSymbolIndex <= 0 ) return false; 718 719 if (requireDot) { 720 int dotIndex = s.lastIndexOf('.'); 721 if (dotIndex == -1) return false; if (dotIndex < atSymbolIndex + 2) return false; if (dotIndex == s.length() - 1 ) return false; } 725 726 return true; 727 } 728 729 733 public static boolean isUrl(String s) { 734 if (isEmpty(s)) return defaultEmptyOK; 735 if (s.indexOf("://") != -1) 736 return true; 737 return false; 738 } 739 740 746 public static boolean isYear(String s) { 747 if (isEmpty(s)) return defaultEmptyOK; 748 749 if (!isNonnegativeInteger(s)) return false; 750 return ((s.length() == 2) || (s.length() == 4)); 751 } 752 753 756 public static boolean isIntegerInRange(String s, int a, int b) { 757 if (isEmpty(s)) return defaultEmptyOK; 758 if (!isSignedInteger(s)) return false; 761 int num = Integer.parseInt(s); 766 767 return ((num >= a) && (num <= b)); 768 } 769 770 771 public static boolean isMonth(String s) { 772 if (isEmpty(s)) return defaultEmptyOK; 773 return isIntegerInRange(s, 1, 12); 774 } 775 776 777 public static boolean isDay(String s) { 778 if (isEmpty(s)) return defaultEmptyOK; 779 return isIntegerInRange(s, 1, 31); 780 } 781 782 783 public static int daysInFebruary(int year) { 784 return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28); 787 } 788 789 790 public static boolean isHour(String s) { 791 if (isEmpty(s)) return defaultEmptyOK; 792 return isIntegerInRange(s, 0, 23); 793 } 794 795 796 public static boolean isMinute(String s) { 797 if (isEmpty(s)) return defaultEmptyOK; 798 return isIntegerInRange(s, 0, 59); 799 } 800 801 802 public static boolean isSecond(String s) { 803 if (isEmpty(s)) return defaultEmptyOK; 804 return isIntegerInRange(s, 0, 59); 805 } 806 807 808 public static boolean isDate(String year, String month, String day) { 809 if (!(isYear(year) && isMonth(month) && isDay(day))) return false; 811 812 int intYear = Integer.parseInt(year); 813 int intMonth = Integer.parseInt(month); 814 int intDay = Integer.parseInt(day); 815 816 if (intDay > daysInMonth[intMonth - 1]) return false; 818 if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false; 819 return true; 820 } 821 822 823 public static boolean isDate(String date) { 824 if (isEmpty(date)) return defaultEmptyOK; 825 String month; 826 String day; 827 String year; 828 829 int dateSlash1 = date.indexOf("/"); 830 int dateSlash2 = date.lastIndexOf("/"); 831 832 if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return false; 833 month = date.substring(0, dateSlash1); 834 day = date.substring(dateSlash1 + 1, dateSlash2); 835 year = date.substring(dateSlash2 + 1); 836 837 return isDate(year, month, day); 838 } 839 840 841 public static boolean isDateAfterToday(String date) { 842 if (isEmpty(date)) return defaultEmptyOK; 843 int dateSlash1 = date.indexOf("/"); 844 int dateSlash2 = date.lastIndexOf("/"); 845 846 if (dateSlash1 <= 0) return false; 847 848 java.util.Date passed = null; 849 if (dateSlash1 == dateSlash2) { 850 String month = date.substring(0, dateSlash1); 852 String day = "28"; 853 String year = date.substring(dateSlash1 + 1); 854 if (!isDate(year, month, day)) return false; 855 856 try { 857 int monthInt = Integer.parseInt(month); 858 int yearInt = Integer.parseInt(year); 859 Calendar calendar = Calendar.getInstance(); 860 calendar.set(yearInt, monthInt - 1, 0, 0, 0, 0); 861 calendar.add(Calendar.MONTH, 1); 862 passed = new java.util.Date (calendar.getTime().getTime()); 863 } catch (Exception e) { 864 passed = null; 865 } 866 } else { 867 String month = date.substring(0, dateSlash1); 868 String day = date.substring(dateSlash1 + 1, dateSlash2); 869 String year = date.substring(dateSlash2 + 1); 870 if (!isDate(year, month, day)) return false; 871 passed = UtilDateTime.toDate(month, day, year, "0", "0", "0"); 872 } 873 874 java.util.Date now = UtilDateTime.nowDate(); 875 if (passed != null) { 876 return passed.after(now); 877 } else { 878 return false; 879 } 880 } 881 882 883 public static boolean isTime(String hour, String minute, String second) { 884 if (isHour(hour) && isMinute(minute) && isSecond(second)) 886 return true; 887 else 888 return false; 889 } 890 891 892 public static boolean isTime(String time) { 893 if (isEmpty(time)) return defaultEmptyOK; 894 895 String hour; 896 String minute; 897 String second; 898 899 int timeColon1 = time.indexOf(":"); 900 int timeColon2 = time.lastIndexOf(":"); 901 902 if (timeColon1 <= 0) return false; 903 hour = time.substring(0, timeColon1); 904 if (timeColon1 == timeColon2) { 905 minute = time.substring(timeColon1 + 1); 906 second = "0"; 907 } else { 908 minute = time.substring(timeColon1 + 1, timeColon2); 909 second = time.substring(timeColon2 + 1); 910 } 911 return isTime(hour, minute, second); 912 } 913 914 919 public static boolean isValueLinkCard(String stPassed) { 920 if (isEmpty(stPassed)) return defaultEmptyOK; 921 String st = stripCharsInBag(stPassed, creditCardDelimiters); 922 if (st.length() == 16 && (st.startsWith("7") || st.startsWith("6"))) { 923 return true; 924 } 925 return false; 926 } 927 928 933 public static boolean isOFBGiftCard(String stPassed) { 934 if (isEmpty(stPassed)) return defaultEmptyOK; 935 String st = stripCharsInBag(stPassed, creditCardDelimiters); 936 if (st.length() == 15 && sumIsMod10(getLuhnSum(st))) { 937 return true; 938 } 939 return false; 940 } 941 942 947 public static boolean isGiftCard(String stPassed) { 948 if (isOFBGiftCard(stPassed)) { 949 return true; 950 } else if (isValueLinkCard(stPassed)) { 951 return true; 952 } 953 return false; 954 } 955 956 public static int getLuhnSum(String stPassed) { 957 stPassed = stPassed.replaceAll("\\D", ""); 959 int len = stPassed.length(); 960 int sum = 0; 961 int mul = 1; 962 for (int i = len - 1; i >= 0; i--) { 963 int digit = Character.digit(stPassed.charAt(i), 10); 964 digit *= (mul == 1) ? mul++ : mul--; 965 sum += (digit >= 10) ? (digit % 10) + 1 : digit; 966 } 967 968 return sum; 969 } 970 971 public static int getLuhnCheckDigit(String stPassed) { 972 int sum = getLuhnSum(stPassed); 973 int mod = ((sum / 10 + 1) * 10 - sum) % 10; 974 return (10 - mod); 975 } 976 977 public static boolean sumIsMod10(int sum) { 978 return ((sum % 10) == 0); 979 } 980 981 public static String appendCheckDigit(String stPassed) { 982 String checkDigit = new Integer (getLuhnCheckDigit(stPassed)).toString(); 983 return stPassed + checkDigit; 984 } 985 986 991 public static boolean isCreditCard(String stPassed) { 992 if (isEmpty(stPassed)) return defaultEmptyOK; 993 String st = stripCharsInBag(stPassed, creditCardDelimiters); 994 995 if (st.length() > 19) return false; 997 return sumIsMod10(getLuhnSum(st)); 998 } 999 1000 1005 public static boolean isVisa(String cc) { 1006 if (((cc.length() == 16) || (cc.length() == 13)) && (cc.substring(0, 1).equals("4"))) 1007 return isCreditCard(cc); 1008 return false; 1009 } 1010 1011 1016 public static boolean isMasterCard(String cc) { 1017 int firstdig = Integer.parseInt(cc.substring(0, 1)); 1018 int seconddig = Integer.parseInt(cc.substring(1, 2)); 1019 1020 if ((cc.length() == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) 1021 return isCreditCard(cc); 1022 return false; 1023 1024 } 1025 1026 1030 public static boolean isAmericanExpress(String cc) { 1031 int firstdig = Integer.parseInt(cc.substring(0, 1)); 1032 int seconddig = Integer.parseInt(cc.substring(1, 2)); 1033 1034 if ((cc.length() == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) 1035 return isCreditCard(cc); 1036 return false; 1037 1038 } 1039 1040 1044 public static boolean isDinersClub(String cc) { 1045 int firstdig = Integer.parseInt(cc.substring(0, 1)); 1046 int seconddig = Integer.parseInt(cc.substring(1, 2)); 1047 1048 if ((cc.length() == 14) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8))) 1049 return isCreditCard(cc); 1050 return false; 1051 } 1052 1053 1057 public static boolean isCarteBlanche(String cc) { 1058 return isDinersClub(cc); 1059 } 1060 1061 1065 public static boolean isDiscover(String cc) { 1066 String first4digs = cc.substring(0, 4); 1067 1068 if ((cc.length() == 16) && (first4digs.equals("6011"))) 1069 return isCreditCard(cc); 1070 return false; 1071 } 1072 1073 1077 public static boolean isEnRoute(String cc) { 1078 String first4digs = cc.substring(0, 4); 1079 1080 if ((cc.length() == 15) && (first4digs.equals("2014") || first4digs.equals("2149"))) 1081 return isCreditCard(cc); 1082 return false; 1083 } 1084 1085 1089 public static boolean isJCB(String cc) { 1090 String first4digs = cc.substring(0, 4); 1091 1092 if ((cc.length() == 16) && 1093 (first4digs.equals("3088") || 1094 first4digs.equals("3096") || 1095 first4digs.equals("3112") || 1096 first4digs.equals("3158") || 1097 first4digs.equals("3337") || 1098 first4digs.equals("3528"))) 1099 return isCreditCard(cc); 1100 return false; 1101 } 1102 1103 1107 public static boolean isAnyCard(String ccPassed) { 1108 if (isEmpty(ccPassed)) return defaultEmptyOK; 1109 1110 String cc = stripCharsInBag(ccPassed, creditCardDelimiters); 1111 1112 if (!isCreditCard(cc)) return false; 1113 if (isMasterCard(cc) || isVisa(cc) || isAmericanExpress(cc) || isDinersClub(cc) || 1114 isDiscover(cc) || isEnRoute(cc) || isJCB(cc)) 1115 return true; 1116 return false; 1117 } 1118 1119 1123 public static String getCardType(String ccPassed) { 1124 if (isEmpty(ccPassed)) return "Unknown"; 1125 String cc = stripCharsInBag(ccPassed, creditCardDelimiters); 1126 1127 if (!isCreditCard(cc)) return "Unknown"; 1128 1129 if (isMasterCard(cc)) return "MasterCard"; 1130 if (isVisa(cc)) return "Visa"; 1131 if (isAmericanExpress(cc)) return "AmericanExpress"; 1132 if (isDinersClub(cc)) return "DinersClub"; 1133 if (isDiscover(cc)) return "Discover"; 1134 if (isEnRoute(cc)) return "EnRoute"; 1135 if (isJCB(cc)) return "JCB"; 1136 return "Unknown"; 1137 } 1138 1139 1144 public static boolean isCardMatch(String cardType, String cardNumberPassed) { 1145 if (isEmpty(cardType)) return defaultEmptyOK; 1146 if (isEmpty(cardNumberPassed)) return defaultEmptyOK; 1147 String cardNumber = stripCharsInBag(cardNumberPassed, creditCardDelimiters); 1148 1149 if ((cardType.equalsIgnoreCase("VISA")) && (isVisa(cardNumber))) return true; 1150 if ((cardType.equalsIgnoreCase("MASTERCARD")) && (isMasterCard(cardNumber))) return true; 1151 if (((cardType.equalsIgnoreCase("AMERICANEXPRESS")) || (cardType.equalsIgnoreCase("AMEX"))) && (isAmericanExpress(cardNumber))) return true; 1152 if ((cardType.equalsIgnoreCase("DISCOVER")) && (isDiscover(cardNumber))) return true; 1153 if ((cardType.equalsIgnoreCase("JCB")) && (isJCB(cardNumber))) return true; 1154 if (((cardType.equalsIgnoreCase("DINERSCLUB")) || (cardType.equalsIgnoreCase("DINERS"))) && (isDinersClub(cardNumber))) return true; 1155 if ((cardType.equalsIgnoreCase("CARTEBLANCHE")) && (isCarteBlanche(cardNumber))) return true; 1156 if ((cardType.equalsIgnoreCase("ENROUTE")) && (isEnRoute(cardNumber))) return true; 1157 return false; 1158 } 1159 1160 1161 1162 public static boolean isNotPoBox(String s) { 1163 if (isEmpty(s)) return defaultEmptyOK; 1164 1165 1175 String sl = s.toLowerCase(); 1176 if (sl.indexOf("p.o. b") != -1) return false; 1177 if (sl.indexOf("p.o.b") != -1) return false; 1178 if (sl.indexOf("p.o b") != -1) return false; 1179 if (sl.indexOf("p o b") != -1) return false; 1180 if (sl.indexOf("po b") != -1) return false; 1181 if (sl.indexOf("pobox") != -1) return false; 1182 if (sl.indexOf("po#") != -1) return false; 1183 if (sl.indexOf("po #") != -1) return false; 1184 1185 if (sl.indexOf("p.0. b") != -1) return false; 1187 if (sl.indexOf("p.0.b") != -1) return false; 1188 if (sl.indexOf("p.0 b") != -1) return false; 1189 if (sl.indexOf("p 0 b") != -1) return false; 1190 if (sl.indexOf("p0 b") != -1) return false; 1191 if (sl.indexOf("p0box") != -1) return false; 1192 if (sl.indexOf("p0#") != -1) return false; 1193 if (sl.indexOf("p0 #") != -1) return false; 1194 return true; 1195 } 1196} 1197 | Popular Tags |