<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 正則表達式 – 信用卡號驗證 > 原文: [https://howtodoinjava.com/regex/java-regex-validate-credit-card-numbers/](https://howtodoinjava.com/regex/java-regex-validate-credit-card-numbers/) 在此 Java [正則表達式教程](https://howtodoinjava.com/java-regular-expression-tutorials/)中,我們將學習**使用正則表達式來驗證信用卡號**。 我們將從多個提供商(例如 VISA,Mastercard,Amex 和 Diners 等)了解號碼格式和信用卡號驗證。 ## 1\. 有效的信用卡號格式 在實際的信用卡上,壓紋卡號的數字通常分為四組。 這使得卡號更易于人類閱讀。 每個信用卡公司都使用此數字格式。 我們將利用每個公司之間的格式差異來允許用戶輸入數字而無需指定公司。 可以從號碼中確定公司。 每個公司的格式為: * **Visa**:13 或 16 位數字,以 4 開頭。 * **Mastercard**:從 51 到 55 開頭的 16 位數字。 * **Discover**:從 6011 或 65 開始的 16 位數字。 * **American Express**:15 位數字,以 34 或 37 開頭。 * **Diners Club** :14 位數字,從 300 到 305、36 或 38 開頭。 * **JCB** :15 位數字,以 2131 或 1800 開頭,或 16 位數字,以 35 開頭。 下面給出的正則表達式假設在執行有效數字檢查之前,我們將明確搜索并替換所有空格和連字符。 輸入中去除了空格和連字符后,下一個正則表達式將檢查信用卡號是否使用六家主要信用卡公司中的任何一家的格式。 它使用命名捕獲**來檢測客戶擁有的信用卡品牌**。 如果不需要確定卡的類型,則可以刪除圍繞每種卡類型的模式的六個捕獲組,因為它們沒有任何其他用途。 如果您僅接受某些品牌的信用卡,則可以從正則表達式中刪除不接受的信用卡。 例如,刪除 JCB 時,請確保刪除最后剩余的“`|`” 在正則表達式中也是如此。 如果您的正則表達式以“`|`”結尾,它也將接受空字符串作為有效的卡號。 ```java Regex : ^(?:(?<visa>4[0-9]{12}(?:[0-9]{3})?)| (?<mastercard>5[1-5][0-9]{14})| (?<discover>6(?:011|5[0-9]{2})[0-9]{12})| (?<amex>3[47][0-9]{13})| (?<diners>3(?:0[0-5]|[68][0-9])?[0-9]{11})| (?<jcb>(?:2131|1800|35[0-9]{3})[0-9]{11}))$ ``` 在此 [**Wiki 頁面**](https://en.wikipedia.org/wiki/Bank_card_number "credit card formats")中詳細了解信用卡號碼格式。 ## 2\. 信用卡號碼驗證示例 ```java public static void main(String[] args) { List<String> cards = new ArrayList<String>(); //Valid Credit Cards cards.add("xxxx-xxxx-xxxx-xxxx"); //Masked to avoid any inconvenience unknowingly //Invalid Credit Card cards.add("xxxx-xxxx-xxxx-xxxx"); //Masked to avoid any inconvenience unknowingly String regex = "^(?:(?<visa>4[0-9]{12}(?:[0-9]{3})?)|" + "(?<mastercard>5[1-5][0-9]{14})|" + "(?<discover>6(?:011|5[0-9]{2})[0-9]{12})|" + "(?<amex>3[47][0-9]{13})|" + "(?<diners>3(?:0[0-5]|[68][0-9])?[0-9]{11})|" + "(?<jcb>(?:2131|1800|35[0-9]{3})[0-9]{11}))$"; Pattern pattern = Pattern.compile(regex); for (String card : cards) { //Strip all hyphens card = card.replaceAll("-", ""); //Match the card Matcher matcher = pattern.matcher(card); System.out.println(matcher.matches()); if(matcher.matches()) { //If card is valid then verify which group it belong System.out.println(matcher.group("mastercard")); } } ``` ## 3\. 使用 Luhn 算法進行校驗和驗證 在處理順序之前,您可以對信用卡號進行額外的驗證檢查。 信用卡號的最后一位數字是根據 [**Luhn 算法**](https://en.wikipedia.org/wiki/Luhn_algorithm "Luhn algorithm")計算得出的校驗和。 由于此算法需要基本的算術運算,因此無法使用正則表達式來實現。 下面是可用于使用 Luhn 算法運行校驗和驗證的方法。 此函數采用一個以信用卡號為參數的字符串。 卡號只能由數字組成。 實際算法在數字數組上運行,計算校驗和。 如果**總和模 10 為零,則卡號有效**。 如果不是,則該數字無效。 我從 [**Google 代碼**](https://code.google.com/p/gnuc-credit-card-checker/source/browse/trunk/CCCheckerPro/src/com/gnuc/java/ccc/Luhn.java "luhn algo")中獲取了 Luhn Algo 的參考實現。 ```java public class Luhn { public static boolean Check(String ccNumber) { int sum = 0; boolean alternate = false; for (int i = ccNumber.length() - 1; i >= 0; i--) { int n = Integer.parseInt(ccNumber.substring(i, i + 1)); if (alternate) { n *= 2; if (n > 9) { n = (n % 10) + 1; } } sum += n; alternate = !alternate; } return (sum % 10 == 0); } } ``` 如果需要,可以隨意修改上述代碼示例以匹配上述正則表達式中的其他驗證規則。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看