Created
June 23, 2013 18:49
-
-
Save iox/5846068 to your computer and use it in GitHub Desktop.
Validando DNIs en Rails 3
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
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