Neftali [Germán.Estévez]
20-06-2006, 13:25:29
Es simple, calcula la letra del NIF (España).
function NIF(DNI: String): Char;
const
STR_CHARS = 'TRWAGMYFPDXBNJZSQVHLCKET';
begin
Result := Copy(STR_CHARS, (StrToInt(DNI) mod 23) + 1, 1)[1];
end;
Gracias a Seoane se añade una funcion para validar el NIF, le pasamos como parametro el documento entero incluido letras y nos devuelve TRUE si es correcto.
function EsNIFCorrecto(NIF: String): Boolean;
var
Numero: Integer;
begin
Result:= FALSE;
if Length(NIF) = 9 then
begin
// Normal
if TryStrToInt(Copy(NIF,1,Length(NIF)-1),Numero) then
Result:= Uppercase(Copy(NIF,Length(NIF),1)) = LetraNIF(Numero);
// Extranjero
if Uppercase(Copy(NIF,1,1)) = 'X' then
if TryStrToInt(Copy(NIF,2,Length(NIF)-2),Numero) then
Result:= Uppercase(Copy(NIF,Length(NIF),1)) = LetraNIF(Numero);
end;
end;
Ejemplo de uso:
if EsNIFCorrecto('12345678Z') then
ShowMessage('Correcto')
else
ShowMessage('Incorrecto');
function NIF(DNI: String): Char;
const
STR_CHARS = 'TRWAGMYFPDXBNJZSQVHLCKET';
begin
Result := Copy(STR_CHARS, (StrToInt(DNI) mod 23) + 1, 1)[1];
end;
Gracias a Seoane se añade una funcion para validar el NIF, le pasamos como parametro el documento entero incluido letras y nos devuelve TRUE si es correcto.
function EsNIFCorrecto(NIF: String): Boolean;
var
Numero: Integer;
begin
Result:= FALSE;
if Length(NIF) = 9 then
begin
// Normal
if TryStrToInt(Copy(NIF,1,Length(NIF)-1),Numero) then
Result:= Uppercase(Copy(NIF,Length(NIF),1)) = LetraNIF(Numero);
// Extranjero
if Uppercase(Copy(NIF,1,1)) = 'X' then
if TryStrToInt(Copy(NIF,2,Length(NIF)-2),Numero) then
Result:= Uppercase(Copy(NIF,Length(NIF),1)) = LetraNIF(Numero);
end;
end;
Ejemplo de uso:
if EsNIFCorrecto('12345678Z') then
ShowMessage('Correcto')
else
ShowMessage('Incorrecto');