From fe90c48dcc71ef452da9e624fd17e85451611182 Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Wed, 4 Feb 2015 12:14:22 +0100 Subject: [PATCH] Evita que valide subcadenas dentro del valor dado MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Añadidos ^ y $ a las expresiones regulares para garantizar que toda la cadena es válida. --- lib/spanish_vat_validators.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/spanish_vat_validators.rb b/lib/spanish_vat_validators.rb index 338332a..6d0b63e 100644 --- a/lib/spanish_vat_validators.rb +++ b/lib/spanish_vat_validators.rb @@ -12,7 +12,7 @@ def message(kind='vat') def validate_nif(v) return false if v.nil? || v.empty? value = v.upcase - return false unless value.match(/[0-9]{8}[a-z]/i) + return false unless value.match(/^[0-9]{8}[a-z]$/i) letters = "TRWAGMYFPDXBNJZSQVHLCKE" check = value.slice!(value.length - 1..value.length - 1) calculated_letter = letters[value.to_i % 23].chr @@ -23,7 +23,7 @@ def validate_nif(v) def validate_cif(v) return false if v.nil? || v.empty? value = v.clone - return false unless value.match(/[a-wyz][0-9]{7}[0-9a-z]/i) + return false unless value.match(/^[a-wyz][0-9]{7}[0-9a-z]$/i) pares = 0 impares = 0 uletra = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"] @@ -57,7 +57,7 @@ def validate_cif(v) def validate_nie(v) return false if v.nil? || v.empty? value = v.upcase - return false unless value.match(/[xyz][0-9]{7}[a-z]/i) + return false unless value.match(/^[xyz][0-9]{7}[a-z]$/i) value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]] validate_nif(value) end