Ver Mensaje Individual
  #1  
Antiguo 20-06-2006
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.264
Reputación: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
Calcular la letra del NIF (España)

Es simple, calcula la letra del NIF (España).

Código Delphi [-]
  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.

Código Delphi [-]
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:

Código Delphi [-]
  if EsNIFCorrecto('12345678Z') then
    ShowMessage('Correcto')
  else
    ShowMessage('Incorrecto');
Responder Con Cita