Skip to content

Instantly share code, notes, and snippets.

@luikore
Last active Aug 29, 2015
Embed
What would you like to do?
Verify Chinese ID card number
def cn_id_verify id, gender=nil
return false if id !~ /\A\d{17}[\dX]\z/i
r = 0
x = 1
id.chars.take(17).reverse_each {|a|
x = (x * 2) % 11
r += a.to_i * x
}
last = (12 - r % 11)
last = 'X' if last == 10
id[17].upcase == last.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment