Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Mi rutina para validar ingreso de datos (https://www.clubdelphi.com/foros/showthread.php?t=96624)

darkamerico 01-03-2024 14:49:33

Mi rutina para validar ingreso de datos
 
Saludos amigos, aquí comparto mi rutina para validar de forma sencilla el ingreso de un conjunto personalizado de datos en un componente TEdit:

Código Delphi [-]
function TFunciones.ValidarEntrada(Key:char; tipoEntrada: string):char;
begin
  if tipoEntrada='SoloLetras' then
  begin
    if not (key in ['a'..'z','A'..'Z',' ', #7, #8, #13]) then Result := #0
    else Result:=key;
  end;
  if tipoEntrada='SoloNumerosEnteros' then
  begin
    if not (key in [#8,'0'..'9','+','-',#13]) then Result:=#0
    else Result:=key;
  end;
  if tipoEntrada='SoloNumerosDecimales' then
  begin
    if not (Key in ['0'..'9','.',#8]) then Result:=#0
    else Result:=key;
  end;
  if tipoEntrada='LetrasYNumeros' then
  begin
    if not (Key in ['0'..'9','.','a'..'z','A'..'Z',' ', #7, #8]) then Result:=#0
    else Result:=key;
  end;
end;

Uso:
Código Delphi [-]
procedure TfrmBuscaSuministro.txtBuscaSumKeyPress(Sender: TObject; var Key: Char);
begin
  key:=cls.ValidarEntrada(Key, 'SoloNumerosEnteros');
end;

Si hubieran mejores propuestas por favor agradecería las sugerencias.

Atte

mamcx 01-03-2024 16:14:30

Una mejora simple es dejar de usar `stringly typed apis`:

https://wiki.c2.com/?StringlyTyped
Cita:

Method parameters that take strings when other more appropriate types should be used
Aqui es claro que un enum seria mejor.

MAXIUM 03-03-2024 17:08:48

De a poco agarrando gusto por ChatGPT. Usando Enum, evitas errores.

Código Delphi [-]
type
  TTipoEntrada = (teSoloLetras, teSoloNumerosEnteros, teSoloNumerosDecimales, teLetrasYNumeros);

function TFunciones.ValidarEntrada(Key: Char; Tipo: TTipoEntrada): Char;
begin
  case Tipo of
    teSoloLetras:
      if not (Key in ['a'..'z', 'A'..'Z', ' ', #7, #8, #13]) then Result := #0
      else Result := Key;
    teSoloNumerosEnteros:
      if not (Key in [#8, '0'..'9', '+', '-', #13]) then Result := #0
      else Result := Key;
    teSoloNumerosDecimales:
      if not (Key in ['0'..'9', '.', #8]) then Result := #0
      else Result := Key;
    teLetrasYNumeros:
      if not (Key in ['0'..'9', '.', 'a'..'z', 'A'..'Z', ' ', #7, #8]) then Result := #0
      else Result := Key;
  end;
end;

Neftali [Germán.Estévez] 04-03-2024 09:33:33

Cita:

Empezado por darkamerico (Mensaje 554686)
Saludos amigos, aquí comparto mi rutina para validar de forma sencilla el ingreso de un conjunto personalizado de datos en un componente TEdit:
Si hubieran mejores propuestas por favor agradecería las sugerencias.


Gracias.
^\||/^\||/^\||/^\||/

cloayza 04-03-2024 16:08:31

Si me lo permiten, propongo una pequeña variante...

Código Delphi [-]

function TFunciones.ValidarEntrada(Key: Char; Tipo: TTipoEntrada): Char;
const
  ACheckingKey:Array[TTipoEntrada] of String=('abcdefghijklmnopqrstuvwxyz'+#7+#8+#9,
                                           '0123456789+-'#8+#13,
                                           '0123456789.'+#8,
                                           '0123456789abcdefghijklmnopqrstuvwxyz '+#7+#8);
begin
  result:=#0;
  if Pos(lowercase(key), ACheckingKey[Tipo])>0 then
     result:=Key;
end;
Saludos cordiales

darkamerico 03-05-2024 00:25:22

Gracias por sus aportes
 
Interesantes ideas chicos, felicitaciones a todos.


La franja horaria es GMT +2. Ahora son las 11:34:11.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi