Last active
August 29, 2015 14:12
-
-
Save luikore/cc42d6eeec6f5f1e389b to your computer and use it in GitHub Desktop.
Verify Chinese ID card number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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