A unique set Charcan be used for your purpose.
function GetValueTrat(const aValue: string): string;
const
CHARS = ['0'..'9', 'a'..'z', 'A'..'Z'];
var
i: Integer;
begin
Result := aValue.Trim;
for i := 1 to Length(Result) do
begin
if not (Result[i] in CHARS) then
raise Exception.Create('Non valido');
end;
end;
Please note that in your function, if it aValuecontains a whitespace character, for example 'test value ', for example, an exception occurs, so use is Trimuseless after the instruction if.
^[0-9a-zA-Z] , .
@RBA , System.Character.TCharHelper.IsLetterOrDigit :
if not Result[i].IsLetterOrDigit then
raise Exception.Create('Non valido');