Jump to content

isDni validation


Recommended Posts

Hi there,


I have configured support for requesting DNI to the customer. I'm running 1.4.3


I've found that 1.4.3 uses a isDniLite function to do the validation which is extremely basic. It doesn't perform almost any validation.


The older Validate::isDni function has been deprecated (see relevant DNI changes below). Are there any plans to support a good DNI validation?


[-] FO : fixed bug #PSCFI-1310 - DNI mandatory for country not requesting DNI

[-] Installer : fixed alter table address dni can be null

[-] Installer : fix update (dni field can be null)

[~] Deprecated : CustomerCore::getNeedDNI([...])

[~] Deprecated : ValidateCore::isDniBool([...])

[*] BO : $validateDni is a boolean, but was compared to integer for display error message

[-] FO : fixed bug #7492 - Not valid DNI raises two errors instead of one

[-] FO : now Validate::isDni() return true for empty value

[-] Project : Now the dni check is compatible with more format

[-] FO : adding missing check for DNI field

[-] BO : Validation of dni was false (return negative value to ObjectModel), Bug #5932 fixed



Link to comment
Share on other sites

  • 1 year later...

Dándole mil vueltas lo he conseguido, en authcontroller.php cambias isdnilite() por isdni() y en validate.php cambias los return de 1 a true y todos los demás a false, para devolver varias respuestas antes devolvía un integer y necesita un bool.


ha de quedar así




if (Country::isNeedDniByCountryId($address->id_country) AND (!Tools::getValue('dni') OR !Validate::isDni(Tools::getValue('dni'))))

$this->errors[] = Tools::displayError('Identification number is incorrect or has already been used.');

elseif (!Country::isNeedDniByCountryId($address->id_country))

$address->dni = NULL;




public static function isDni($dni)



Return code:

1 : It's Ok

0 : Bad format for DNI

-1 : DNI duplicate

-2 : NIF error

-3 : CIF error

-4 : NIE error





if (!$dni)

return true;


$dni = strtoupper($dni);

if (!preg_match('/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/', $dni))

return false;


$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('

SELECT `id_address`

FROM `'._DB_PREFIX_.'address`

WHERE `dni` = \''.pSQL($dni).'\'');

if ($result)

return false;


for ($i=0;$i<9;$i++)

$char[$i] = substr($dni, $i, 1);

// 12345678T

if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $dni))

if ($char[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($dni, 0, 8) % 23, 1))

return true;


return false;


$sum = $char[2] + $char[4] + $char[6];

for ($i = 1; $i < 8; $i += 2)

$sum += substr((2 * $char[$i]),0,1) + substr((2 * $char[$i]),1,1);


$n = 10 - substr($sum, strlen($sum) - 1, 1);


if (preg_match('/^[KLM]{1}/', $dni))

if ($char[8] == chr(64 + $n))

return true;


return false;


if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $dni))

if ($char[8] == chr(64 + $n) || $char[8] == substr($n, strlen($n) - 1, 1))

return true;


return false;


if (preg_match('/^[T]{1}/', $dni))

if ($char[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $dni))

return true;


return false;


if (preg_match('/^[XYZ]{1}/', $dni))

if ($char[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X','Y','Z'), array('0','1','2'), $dni), 0, 8) % 23, 1))

return true;


return false;


return false;



Espero que te sirva

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...