over 3 years ago

系統:

Mac OS X Yosemite 10.10.1
Ruby 2.1.5
Rails 4.1.8

相關連結

中華民國身分證驗證規則 GOTO

設定步驟如下

1. 新增存放 validators 資料夾

app/validators

2. 新增自製 Validator

驗證中華民國身分證

app/validators/identity_validator.rb

class IdentityValidator < ActiveModel::EachValidator
  CITY_POINT = { A:10, B:11, C:12, D:13, E:14, F:15, G:16, H:17, I:34, J:18, K:19, M:21, N:22, O:35, P:23, Q:24, T:27, U:28, V:29, W:32, X:30, Z:33 }
  MULTIPLIER = '19876543211'

  def validate_each(record, attribute, value)
    is_valid = false
    if value =~ /^[a-zA-Z][12][0-9]{8}$/
      id_multiplier = CITY_POINT[value.chars.first.upcase.to_sym].to_s + value[1..9]
      total = 0
      id_multiplier.chars.each_with_index do |value, index|
        total = total + value.to_i * MULTIPLIER.chars.at(index).to_i
      end
      is_valid = true if total % 10 == 0
    end
    unless is_valid
      record.errors.add(attribute, :wrong_identity, options) unless is_valid
    end
  end
end

3. Model 設定

app/models/profile.rb

  ...
  validates :cid, identity: true, allow_blank: true
  ...

4. locale 設定

config/locales/model.profile.zh-TW.yml

zh-TW:
  ...
  # errors messages
  activerecord:
    errors:
      messages:
        wrong_identity: '身分證格式錯誤'
← Devise - part 3 - Add User Profile with avatar Use fancyBox 2 in Ruby on Rails →
 
comments powered by Disqus