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
(String stPassed)
| 855 | * @return true, if the credit card number passes the Luhn Mod-10 test, false otherwise |
| 856 | */ |
| 857 | public static boolean isCreditCard(String stPassed) { |
| 858 | if (isEmpty(stPassed)) return defaultEmptyOK; |
| 859 | String st = stripCharsInBag(stPassed, creditCardDelimiters); |
| 860 | |
| 861 | int sum = 0; |
| 862 | int mul = 1; |
| 863 | int l = st.length(); |
| 864 | |
| 865 | // Encoding only works on cards with less than 19 digits |
| 866 | if (l > 19) return (false); |
| 867 | for (int i = 0; i < l; i++) { |
| 868 | String digit = st.substring(l - i - 1, l - i); |
| 869 | int tproduct = 0; |
| 870 | |
| 871 | try { |
| 872 | tproduct = Integer.parseInt(digit, 10) * mul; |
| 873 | } catch (Exception e) { |
| 874 | Debug.logWarning(e.getMessage()); |
| 875 | return false; |
| 876 | } |
| 877 | if (tproduct >= 10) |
| 878 | sum += (tproduct % 10) + 1; |
| 879 | else |
| 880 | sum += tproduct; |
| 881 | if (mul == 1) |
| 882 | mul++; |
| 883 | else |
| 884 | mul--; |
| 885 | } |
| 886 | // Uncomment the following line to help create credit card numbers |
| 887 | // 1. Create a dummy number with a 0 as the last digit |
| 888 | // 2. Examine the sum written out |
| 889 | // 3. Replace the last digit with the difference between the sum and |
| 890 | // the next multiple of 10. |
| 891 | |
| 892 | // document.writeln("<BR>Sum = ",sum,"<BR>"); |
| 893 | // alert("Sum = " + sum); |
| 894 | |
| 895 | if ((sum % 10) == 0) |
| 896 | return true; |
| 897 | else |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | /** Checks to see if the cc number is a valid Visa number |
| 902 | * |
no test coverage detected