Skip to content

Instantly share code, notes, and snippets.

@iox
Created June 23, 2013 18:49
Show Gist options
  • Save iox/5846068 to your computer and use it in GitHub Desktop.
Save iox/5846068 to your computer and use it in GitHub Desktop.
Validando DNIs en Rails 3
validates_format_of :dni, :with => /\A[0-9]{8}[A-Za-z]\Z/, :on => :update, :message => "El DNI tiene que tener un formato como el siguiente: 12345678A"
validate :dni_letter_must_match_number
def dni_letter_must_match_number
if "TRWAGMYFPDXBNJZSQVHLCKE"[dni[0..7].to_i % 23].chr != dni[8]
errors.add(:dni, "La letra no coincide con el número")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment