osCommerce Solutions

Validar NIF, CIF, NIE



1) Abrir catalog/includes/functions/general.php

	Aadir antes del ?> final lo siguiente:

	function tep_valida_nif_cif_nie($cif) {
	//returns: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF bad, -2 = CIF bad, -3 = NIE bad, 0 = ??? bad
	//funcin creada por David Vidal Serra, Copyleft 2005
        $cif=strtoupper($cif);
        if (!ereg('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)',$cif)) {return 0;}
        for ($i=0;$i<9;$i++) {$num[$i]=substr($cif,$i,1);}
        $suma=$num[2]+$num[4]+$num[6];
        for ($i=1;$i<8;$i+=2) {$suma+=substr((2*$num[$i]),0,1)+substr((2*$num[$i]),1,1);}
        $n=10-substr($suma,strlen($suma)-1,1);
        if (ereg('^[ABCDEFGHNPQS]{1}',$cif)) {
                if ($num[8]==chr(64+$n) || $num[8]==substr($n,strlen($n)-1,1)){return 2;} else {return -2;}}
        if (ereg('^[KLM]{1}',$cif)) {
                if ($num[8]==chr(64+$n)) {return 2;} else {return -2;}}
        if (ereg('^[TX]{1}',$cif)) {
                if ($num[8]==substr('TRWAGMYFPDXBNJZSQVHLCKE',substr(ereg_replace('X','0',$cif),0,8)%23,1) || ereg('^[T]{1}[A-Z0-9]{8}$',$cif)) {return 3;} else {return -3;}}
        if (ereg('(^[0-9]{8}[A-Z]{1}$)',$cif)) {
                if ($num[8]==substr('TRWAGMYFPDXBNJZSQVHLCKE',substr($cif,0,8)%23,1)) {return 1;} else {return -1;}}
        return 0;
      }

2) Abrir catalog/create_account.php

	Localizar el siguiente cdigo:

	//NIF start
    if (ACCOUNT_NIF == 'true'){
      if (($nif == "") && (ACCOUNT_NIF_REQ == 'true')) {
        $error = true;
        $messageStack->add('create_account', ENTRY_NO_NIF_ERROR);
      } else if ((strlen($nif) != 9) && ($nif != ""))  {
        $error = true;
        $messageStack->add('create_account', ENTRY_FORMATO_NIF_ERROR);
      } else if (strlen($nif) == 9) {


3) Sustituir toda la sentencia if por lo siguiente:

	//NIF start
    if (ACCOUNT_NIF == 'true'){
      if (($nif == "") && (ACCOUNT_NIF_REQ == 'true')) {
        $error = true;
        $messageStack->add('create_account', ENTRY_NO_NIF_ERROR);
      } else if ((strlen($nif) != 9) && ($nif != ""))  {
        $error = true;
        $messageStack->add('create_account', ENTRY_FORMATO_NIF_ERROR);
      } else if (strlen($nif) == 9) {
      	$result = tep_valida_nif_cif_nie($nif);

	      if ($result <= 0) {
	      	$error = true;
            	$messageStack->add('create_account', ENTRY_FORMATO_NIF_ERROR);
	      } else {
			//correcto
	      }
	} else {
	      $error = true;
            $messageStack->add('create_account', ENTRY_FORMATO_NIF_ERROR);
	}
    }
    //NIF end

4) Se acab


