C# 中華民國 發票載具檢核(發票載具 檢查)~ 使用正則表達式/正規表達式
C# 中華民國 發票載具檢核(發票載具 檢查) ~ 使用正則表達式/正規表達式
資料來源: https://regex101.com/
https://github.com/jash-git/Regular-Expression-Teaching [正則表達式 影片+PDF 教學 [Regular Expression Teaching]]
[電子發票手機條碼驗證 / 如何驗證手機條碼載具]
https://www.ecpay.com.tw/CascadeFAQ/CascadeFAQ_Qa?nID=3531 (規則)
https://cynthiachuang.github.io/Check-E-Government-Uniform-Invoice/ (JAVA code)
C# 線上編譯器: https://dotnetfiddle.net/
code
using System;
using System.Text.RegularExpressions;
class Program
{
/*
如何驗證手機條碼載具
https://www.ecpay.com.tw/CascadeFAQ/CascadeFAQ_Qa?nID=3531
共通性載具編碼邏輯或載具類別編號與載具編碼不符,將會影響消費者中獎權益
綠界科技僅協助基本檢核邏輯,載具內容正確性需請商家自行驗證,
驗證完畢後再進行建立訂單及開立票動作。
基本檢核邏輯:第1碼為【/】; 其餘7碼則由數字【0-9】大寫英文【A-Z】
與特殊符號【+】【-】【.】這39個字元組成的編號
*/
/*
對應正則表達式/正規表達式
https://cynthiachuang.github.io/Check-E-Government-Uniform-Invoice/
^ -> 匹配字符串的開頭
/ -> 第一個字一定要是斜線
[] -> 描述符合規則範圍
A-Z -> 大寫英文字母
0-9 -> 純數字
+-\. -> 四個特定符號
{7} -> / 之後總共7碼
$ -> 匹配字符串的結尾
*/
static void Main()
{
string strinput="/RZDFEP2";
MatchCollection matches01 = Regex.Matches(strinput, @"^/[A-Z0-9+-\.]{7}$");
if( (matches01!=null) && (matches01.Count>0))
{
Console.WriteLine(@"合法 手機載具");
}
else
{
Console.WriteLine(@"非法 手機載具");
}
}
}
One thought on “C# 中華民國 發票載具檢核(發票載具 檢查)~ 使用正則表達式/正規表達式”
JAVA Code
https://cynthiachuang.github.io/Check-E-Government-Uniform-Invoice/
public static boolean verifyEgui(String egui){
if(egui.length() != 8){
System.out.println("Fail, 長度錯誤");
return false;
} else if(egui.charAt(0) != '/'){
System.out.println("Fail, 必須 / 開頭");
return false;
}
for(int i=1; i=48 && charactercode=65 && charactercode<=90);
if (!(isSymbol || isNumber || isLetter)) {
System.out.println("Fail, 包含不合法字元");
return false;
}
}
return true;
}