跳至內容

身分證字號 (中華民國)

維基教科書,自由的教學讀本

身分證字號 (中華民國)

F228722347

應用

[編輯]

Java

[編輯]
import java.util.HashMap;
import java.util.Map;

public class TaiwanIdCard {
	
	Map<Character, Integer> regions;
	
	public TaiwanIdCard() {
		regions = new HashMap<Character, Integer>();
		regions.put('B', 0);    regions.put('N', 0);    regions.put('Z', 0);
		regions.put('A', 1);    regions.put('M', 1);    regions.put('W', 1);
		regions.put('K', 2);    regions.put('L', 2);    regions.put('Y', 2);
		regions.put('J', 3);    regions.put('V', 3);    regions.put('X', 3);
		regions.put('H', 4);    regions.put('U', 4);
		regions.put('G', 5);    regions.put('T', 5);
		regions.put('F', 6);    regions.put('S', 6);
		regions.put('E', 7);    regions.put('R', 7);
		regions.put('D', 8);    regions.put('O', 8);    regions.put('Q', 8);
		regions.put('C', 9);    regions.put('I', 9);    regions.put('P', 9);
	}
 
	public boolean check(String id) {
		String re = "^[A-Z]{1}[1-2]{1}[0-9]{8}$";
		if (!id.matches(re))
			return false;
		int sum = regions.get(id.charAt(0));
		int m = 8;
		for(int i=1; i<9; i++, m--)
			sum += (Character.getNumericValue(id.charAt(i)) * m);
		sum +=  Character.getNumericValue(id.charAt(9));
		if ((sum % 10) != 0)
			return false;
		return true;   
	}
 
	// gender: 1 for Male, 2 for Female
	public String create(Character region, int gender, int no) {
		StringBuilder id = new StringBuilder();
		if (!regions.containsKey(region) ||
			!((gender == 1) || (gender == 2)) ||
			!((no >= 0) && (no <= 9999999))) {
			// input invalid
			return id.toString();
		} 

		id.append(region).append(gender);
		String strNo = String.valueOf(no);
		for (int i=0; i<(7-strNo.length()); i++)
			id.append('0');  
		id.append(strNo);
 
		int sum = regions.get(id.charAt(0));
		int m = 8;
		for(int i=1; i<9; i++, m--)
			sum += (Character.getNumericValue(id.charAt(i)) * m);
		id.append(10 - (sum % 10));
		
		return id.toString();
	}
}

JavaScript

[編輯]
class TaiwanIdCard {
	constructor() {
		this.regions = {
			'B' : 0,    'N' : 0,    'Z' : 0,
			'A' : 1,    'M' : 1,    'W' : 1,
			'K' : 2,    'L' : 2,    'Y' : 2,
			'J' : 3,    'V' : 3,    'X' : 3,
			'H' : 4,    'U' : 4,
			'G' : 5,    'T' : 5,
			'F' : 6,    'S' : 6,
			'E' : 7,    'R' : 7,
			'D' : 8,    'O' : 8,    'Q' : 8,
			'C' : 9,    'I' : 9,    'P' : 9
		};
	}
  
	check(id) {
		let re = new RegExp('^[A-Z]{1}[1-2]{1}[0-9]{8}$');
		let x = id.match(re);
		if ((x == null) || (x.length != 1))
			return false;
		let sum = this.regions[id[0]];
		let m = 8;
		for(i=1; i<9; i++, m--)
			sum += (parseInt(id[i]) * m);
		sum +=  parseInt(id[9]);
		if ((sum % 10) != 0)
			return false;
		return true;   
	}
	// gender: 1 for Male, 2 for Female
	create(region, gender, no) {
		let id = "";
		if ((typeof region === 'string') && (this.regions[region] != undefined) &&
			((gender == 1) || (gender == 2)) &&
			((no >= 0) && (no <= 9999999))) {
			id = region + gender;
			no = String(parseInt(Number(no)));
			for (i=0; i<(7-no.length); i++)
				id += '0';  
			id += no;
			
			let sum = this.regions[id[0]];
			let m = 8;
			for(i=1; i<9; i++, m--)
				sum += (parseInt(id[i]) * m);
			id += (10 - (sum % 10));
		}
		return id;
	}
}

PHP

[編輯]
<?php  
class TaiwanIdCard {
	
	private $regions;
	
	function __construct() {
		$this->regions = array(
			'B' => 0,    'N' => 0,    'Z' => 0,
			'A' => 1,    'M' => 1,    'W' => 1,
			'K' => 2,    'L' => 2,    'Y' => 2,
			'J' => 3,    'V' => 3,    'X' => 3,
			'H' => 4,    'U' => 4,
			'G' => 5,    'T' => 5,
			'F' => 6,    'S' => 6,
			'E' => 7,    'R' => 7,
			'D' => 8,    'O' => 8,    'Q' => 8,
			'C' => 9,    'I' => 9,    'P' => 9
		);
	}

	function check($id) {
		$re = "/^[A-Za-z][1-2][0-9]{8}$/";
		if (!preg_match($re, $id))
			return false;
		$sum = $this->regions[$id[0]];
		$m = 8;
		for($i=1; $i<9; $i++, $m--) 
			$sum += (intval($id[$i]) * $m);
		$sum +=  intval($id[9]);
		if (($sum % 10) != 0)
			return false;
		return true;   
	}
 
	// gender: 1 for Male, 2 for Female
	function create($region, $gender, $no) {
		$id = "";
		if (is_string($region) && array_key_exists($region, $this->regions) &&
			(($gender == 1) || ($gender == 2)) &&
			(($no >= 0) && ($no <= 9999999))) {
			$id = $region . $gender;
			$no = strval(intval($no));
			for ($i=0; $i<(7-strlen($no)); $i++)
				$id .= '0';  
			$id .= strval($no);
			
			$sum = $this->regions[$id[0]];
			$m = 8;
			for($i=1; $i<9; $i++, $m--)
				$sum += strval(intval($id[$i]) * $m);
			$id .= strval(10 - ($sum % 10));
		}
		return $id;
	}
}

Python

[編輯]
import re 

class TaiwanIdCard:
	regions = {'B':0,'N':0,'Z':0,'A':1,'M':1,'W':1,'K':2,'L':2,'Y':2,'J':3,'V':3,'X':3,'H':4,'U':4,'G':5,'T':5,'F':6,'S':6,'E':7,'R':7,'D':8,'O':8,'Q':8,'C':9,'I':9,'P':9}

	def check(self,id):
		pattern = '^[A-Z]{1}[1-2]{1}[0-9]{8}$'
		x = re.search(pattern, id)
		if x is None:
			return False
		sum = TaiwanIdCard.regions[id[0:1]]
		m = 8
		for i in range(1,9):
			sum += (int(id[i:i+1]) * m)
			m-=1
		sum += int(id[9:10])
		if ((sum % 10) != 0):
			return False
		return True   

	def create(self,region, gender, no):	
		id = ""
		gender = int(gender)
		no = int(no)
		if ((TaiwanIdCard.regions[region] != None) and ((gender == 1) or (gender == 2)) and ((no >= 0) and (no <= 9999999))):
			id = str(region) + str(gender)
			strNo = str(int(no))
			for i in range(0,7-len(strNo)):
			    id += '0'
			id += strNo

			sum = TaiwanIdCard.regions[id[0:1]]
			m = 8
			for i in range(1,9):
				sum += (int(id[i]) * m)
				m-=1
			id += str(10 - (sum % 10))
		return id

VBA

[編輯]
  • File:TaiwanIdCard.cls
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "TaiwanIdCard"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Private regions As Collection

Private Sub Class_Initialize()
    Set regions = New Collection
    With regions
        .Add Item:=0, Key:="B"
        .Add Item:=0, Key:="N"
        .Add Item:=0, Key:="Z"
        
        .Add Item:=1, Key:="A"
        .Add Item:=1, Key:="M"
        .Add Item:=1, Key:="W"
        
        .Add Item:=2, Key:="K"
        .Add Item:=2, Key:="L"
        .Add Item:=2, Key:="Y"
        
        .Add Item:=3, Key:="J"
        .Add Item:=3, Key:="V"
        .Add Item:=3, Key:="X"
        
        .Add Item:=4, Key:="H"
        .Add Item:=4, Key:="U"

        .Add Item:=5, Key:="G"
        .Add Item:=5, Key:="T"
        
        .Add Item:=6, Key:="F"
        .Add Item:=6, Key:="S"

        .Add Item:=7, Key:="E"
        .Add Item:=7, Key:="R"

        .Add Item:=8, Key:="D"
        .Add Item:=8, Key:="O"
        .Add Item:=8, Key:="Q"
        
        .Add Item:=9, Key:="C"
        .Add Item:=9, Key:="I"
        .Add Item:=9, Key:="P"
    End With
End Sub

Private Sub Class_Terminate()
    Set regions = Nothing
End Sub


Public Function check(id As String) As Boolean
    Dim re As RegExp
    Set re = New RegExp       ' 需引用 Microsoft VBScript Regular Expressions 5.5
    re.Pattern = "^[A-Z]{1}[1-2]{1}[0-9]{8}$"
    If Not re.Test(id) Then
        check = False
        Exit Function
    End If
    
    Dim sum As Integer, m As Integer, i As Integer
    sum = regions.Item(Mid(id, 1, 1))
    m = 8
    For i = 2 To 9
        sum = sum + (CInt(Mid(id, i, 1)) * m)
        m = m - 1
    Next i
    sum = sum + CInt(Mid(id, 10, 1))
    
    If ((sum Mod 10) <> 0) Then
        check = False
        Exit Function
    End If
    
    check = True
End Function

' gender: 1 for Male, 2 for Female
Public Function create(region As String, gender As Integer, no As Long) As String

    Dim id As String, strNo As String, i As Integer, m As Integer
    id = ""
        
    If ((regions.Item(region) = Null) Or _
        Not ((gender = 1) Or (gender = 2)) Or _
        Not ((no >= 0) And (no <= 9999999))) Then
        ' input invalid
        create = id
        Exit Function
    End If
 
    id = region & CStr(gender)
    strNo = CStr(no)
    For i = 1 To (7 - Len(strNo))
        id = id & "0"
    Next i
    id = id & strNo
 
    sum = regions.Item(Mid(id, 1, 1))
    m = 8
    For i = 2 To 9
        sum = sum + (CInt(Mid(id, i, 1)) * m)
        m = m - 1
    Next i
    id = id & CStr(10 - (sum Mod 10))
 
    create = id
End Function

外部連結

[編輯]
身分證
居留證