Skip to content

Instantly share code, notes, and snippets.

@luikore
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luikore/cc42d6eeec6f5f1e389b to your computer and use it in GitHub Desktop.
Save luikore/cc42d6eeec6f5f1e389b to your computer and use it in GitHub Desktop.
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