Skip to content

Instantly share code, notes, and snippets.

@sandrocarval
Forked from haggen/cpf_cnpj_validators.rb
Last active August 29, 2015 14:16
Show Gist options
  • Save sandrocarval/9a7062d1d9ddd88236d5 to your computer and use it in GitHub Desktop.
Save sandrocarval/9a7062d1d9ddd88236d5 to your computer and use it in GitHub Desktop.
# CPF and CNPJ validators Rails 3+ style
# By Arthur Corenzan <[email protected]>
# Based on the work of André Camargo <[email protected]>
#
# Copy this file to your initializers or lib directory, then apply like this:
#
# class Person
# ...
#
# validates :cpf, :cpf => true
# validates :cnpj, :cnpj => true
# end
class CpfValidator < ActiveModel::EachValidator
def is_cpf?(value)
match = value.gsub(/\D/, "").scan(/\d/).collect(&:to_i)
if match.size == 11
sum = 10 * match[0] + 9 * match[1] + 8 * match[2] + 7 * match[3] + 6 * match[4] + 5 * match[5] + 4 * match[6] + 3 * match[7] + 2 * match[8]
sum = sum - (11 * (sum / 11))
m = (sum == 0 || sum == 1) ? 0 : 11 - sum
if m == match[9]
sum = match[0] * 11 + match[1] * 10 + match[2] * 9 + match[3] * 8 + match[4] * 7 + match[5] * 6 + match[6] * 5 + match[7] * 4 + match[8] * 3 + match[9] * 2
sum = sum - (11 * (sum / 11))
n = (sum == 0 or sum == 1) ? 0 : 11 - sum
return n == match[10]
end
end
false
end
def validate_each(record, attribute, value)
unless is_cpf?(value)
message = I18n.translate("errors.messages.invalid")
record.errors[attribute] << message
end
end
end
class CnpjValidator < ActiveModel::EachValidator
def is_cnpj?(value)
match = value.gsub(/\D/, "").scan(/\d/).collect(&:to_i)
if match.size == 14
match = match.collect{|x| x.to_i}
sum = match[0] * 5 + match[1] * 4 + match[2] * 3 + match[3] * 2 + match[4] * 9 + match[5] * 8 + match[6] * 7 + match[7] * 6 + match[8] * 5 + match[9] * 4 + match[10] * 3 + match[11] * 2
sum = sum - (11 * (sum / 11))
m = (sum == 0 || sum == 1) ? 0 : 11 - sum
if m == match[12]
sum = match[0] * 6 + match[1] * 5 + match[2] * 4 + match[3] * 3 + match[4] * 2 + match[5] * 9 + match[6] * 8 + match[7] * 7 + match[8] * 6 + match[9] * 5 + match[10] * 4 + match[11] * 3 + match[12] * 2
sum = sum - (11 * (sum / 11))
n = (sum == 0 || sum == 1) ? 0 : 11 - sum
return n == match[13]
end
end
false
end
def validate_each(record, attribute, value)
unless is_cnpj?(value)
message = I18n.translate("errors.messages.invalid")
record.errors[attribute] << message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment