如何驗證身分證字號

如何驗證身分證字號

情境

一般請使用者輸入身分證字號,
這時候就要驗證一下這個身分證號碼是不是合乎規定。

操作流程

輸入正確的身分證號碼


輸入錯誤的身分證號碼


首先要了解的是身分證的規格,可以到下面這個網站看看。
http://my.so-net.net.tw/idealist/Other/SSN.html

程式碼說明

這個程式的layout非常簡單,
單純就放一個EditText跟一個Button就可以 了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/input_id_number"
        android:hint="@string/input_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="@string/check_id"
        android:id="@+id/check_id_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>


接下來在程式當中將這兩個元件拿出來使用。

private Button checkId;
    private EditText inputId;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkId = (Button)findViewById(R.id.check_id_number);
        inputId = (EditText)findViewById(R.id.input_id_number);
        checkId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (checkCardId(inputId.getText().toString())) {
                    Toast.makeText(MainActivity.this, "身分證字號正確", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "身分證字號錯誤,請檢查!", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

接著就是檢查身分證字號的方法了。

//檢查身分證字號
    private boolean checkCardId(String id) {
        if (!id.matches("[a-zA-Z][1-2][0-9]{8}")) {
            return false;
        }

        String newId = id.toUpperCase();
        //身分證第一碼代表數值
        int[] headNum = new int[]{
                1, 10, 19, 28, 37,
                46, 55, 64, 39, 73,
                82, 2, 11, 20, 48,
                29, 38, 47, 56, 65,
                74, 83, 21, 3, 12, 30};

        char[] headCharUpper = new char[]{
                'A', 'B', 'C', 'D', 'E', 'F', 'G',
                'H', 'I', 'J', 'K', 'L', 'M', 'N',
                'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z'
        };

        int index = Arrays.binarySearch(headCharUpper, newId.charAt(0));
        int base = 8;
        int total = 0;
        for (int i = 1; i < 10; i++) {
            int tmp = Integer.parseInt(Character.toString(newId.charAt(i))) * base;
            total += tmp;
            base--;
        }

        total += headNum[index];
        int remain = total % 10;
        int checkNum = (10 - remain) % 10;
        if (Integer.parseInt(Character.toString(newId.charAt(9))) != checkNum) {
            return false;
        }
        return true;
    }

這個方法用來檢查身分證字號, 如果正確就回傳true, 反之就回傳false,
小寫也可以判斷。

程式碼