validate POST-Request

Hi!

I need help, I don’t know how to solve this thing: I am getting a POST-Request and I want to validate the Spanish DNI. I have got the code but I do not know how to implement it the best way. Sorry, I am new to PHP.
[php]
$dni = function(){
$dniUnchecked == $_POST[‘dni’];
if($dniUnchecked == ‘’){
$errors .= ‘Please enter a DNI.
’;
return false;
} else {
if(is_valid_DNI($dniUnchecked)){
$dni = trim($dniUnchecked);
$dni = stripsplashes($dniUnchecked);
$dni = htmlspecialchars($dniUnchecked);
} else {
$errors .= ‘Please enter a valid DNI.
’;
}
}
return $dni;
};

/**

  • Function which checks if a string is a valid spanish DNI

  • @param string $dni

  • @return boolean

  • @author http://www.kiwwito.com/article/php-function-for-spanish-dni-validation
    */
    function is_valid_DNI($dniUnchecked) {
    //Check length
    if (strlen($dniUnchecked) != 9) return false;
    //Possible values for the final letter
    $letterValues = array(
    0 => ‘T’, 1 => ‘R’, 2 => ‘W’, 3 => ‘A’, 4 => ‘G’, 5 => ‘M’,
    6 => ‘Y’, 7 => ‘F’, 8 => ‘P’, 9 => ‘D’, 10 => ‘X’, 11 => ‘B’,
    12 => ‘N’, 13 => ‘J’, 14 => ‘Z’, 15 => ‘S’, 16 => ‘Q’, 17 => ‘V’,
    18 => ‘H’, 19 => ‘L’, 20 => ‘C’, 21 => ‘K’,22 => ‘E’
    );
    //Check if is a DNI
    if (preg_match(’/^[0-9]{8}[A-Z]$/i’, $dniUnchecked)) {
    //Check letter
    if (strtoupper($dniUnchecked[strlen($dniUnchecked) - 1]) !=
    $letterValues[((int) substr($dniUnchecked, 0, strlen($dniUnchecked) - 1)) % 23])
    return false;

     //All was ok
     return true;
    

    }
    }
    [/php]

Thanks/Gracias!

so…what happens when you try it? what error? thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service