i have problem to allow enter only from 1 to 65535 bicouse
example if enter 10000,2000 is ok but enter 99 not.

hire 1 example (not working)

If somthing.Text > '65535 'then
ShowMessage(' you enter biger Number from 65535');
Exit;

hire 2 example (not working)

var
  result : Integer;
  string1: String;
  string2: String;
begin
string1:='65535';
string2:= somthing.Text;
result := AnsiCompareStr(string1, string2);
if result < 0 then ShowMessage(string1+' < '+string2);

please help haw to solve this.
Thanks.

Dani AI

Generated

The root cause was comparing the edit's Text as a string. String comparison is lexical, not numeric — for example "99" compares after "65535" because "9" > "6". Converting the text to a numeric type and then checking the numeric range is the correct fix (as , and hinted).

A compact, robust validation using the RTL's safe parser:

var
  v: Integer;
begin
  if not TryStrToInt(Trim(something.Text), v) then
    ShowMessage('Not a valid integer')
  else if (v < 1) or (v > 65535) then
    ShowMessage('Number must be between 1 and 65535')
  else
    ShowMessage('Number accepted');
end;

Notes and alternatives:

  • Use StrToIntDef(Trim(...), -1) if you prefer a default value instead of TryStrToInt.
  • For typed storage, pick a type that can hold 65535 (for example Word or LongInt) to avoid overflow.
  • To prevent invalid keystrokes, restrict input to digits (and backspace) in OnKeyPress, and validate again in OnExit — never rely on the key filter alone.
  • For a simpler UI, use a numeric control (TSpinEdit / an updown-linked edit) and set Min := 1, Max := 65535; that prevents out-of-range values at entry time.

Trim input before parsing and handle empty strings explicitly. This approach keeps parsing simple, avoids lexical surprises like the one saw, and is safer and clearer than comparing text strings.

Recommended Answers

All 5 Replies

i have problem to allow enter only from 1 to 65535 bicouse
example if enter 10000,2000 is ok but enter 99 not.

hire 1 example (not working)

If somthing.Text > '65535 'then
ShowMessage(' you enter biger Number from 65535');
Exit;

hire 2 example (not working)

var
  result : Integer;
  string1: String;
  string2: String;
begin
string1:='65535';
string2:= somthing.Text;
result := AnsiCompareStr(string1, string2);
if result < 0 then ShowMessage(string1+' < '+string2);

please help haw to solve this.
Thanks.

[DELPHI]

1. Convert your string to a number and test that.

2. create the following function and pass the string as

IF Between(Atextbox.Text,1,65535)
THEN MessageDlg('Thats OK',MtInformation,[MbOK],0)
ELSE MessageDlg('Bad Number',MtInformation,[MbOK],0);

You could also vary the Value by type and overload this to do any numbers

FUNCTION Between(Value : String;Low,High : LongInt) : BOOLEAN;
VAR
N : Integer;
Begin
TRY
N := StrtoInt(Value);
RESULT := ((N >= Low) AND (N <= High));
EXCEPT ON EConvertError DO
RESULT := FALSE;
End;
End;

commented: great funkcion thanks +1

[DELPHI]

1. Convert your string to a number and test that.

2. create the following function and pass the string as

IF Between(Atextbox.Text,1,65535)
THEN MessageDlg('Thats OK',MtInformation,[MbOK],0)
ELSE MessageDlg('Bad Number',MtInformation,[MbOK],0);

You could also vary the Value by type and overload this to do any numbers

FUNCTION Between(Value : String;Low,High : LongInt) : BOOLEAN;
VAR
N : Integer;
Begin
TRY
N := StrtoInt(Value);
RESULT := ((N >= Low) AND (N <= High));
EXCEPT ON EConvertError DO
RESULT := FALSE;
End;
End;

thanks to replay (FUNCTION is ok i have test it ) but i got strange agan i can enter example 2000,23222, but if enter 88 i got error...

I made a pascal source code :D

program sablon;

uses crt;

type range = 1..65535;

const ok = 'ok.';
      error = 'error';
      min = 1;

var one:longint;
    two:range;
    user,temp:string;

function converter(x:string):string;
var code:integer;
begin
     two:=65535;
     {$I-}
     val(x,one,code);

     if (ioresult <> 0) then begin
        converter:=error;
        exit;
     end;
     if (code <> 0) then begin
        converter:=error;
        exit;
     end;
     if (one > two) or (one < min) then converter:=error else converter:=ok;
     {$I+}
end;

begin
     clrscr;
     temp:=error;
     while temp <> ok do begin
           write('please enter a number from 1 to 65535: ');
           readln(user);
           temp:=converter(user);
           writeln(temp);
     end;

     readln;
end.
{created by FlamingClaw 2009}
commented: Great programer Thanks for Help +1

var
xx:Integer;
begin
try
xx:=strtoint(something.text);
if xx<1 then showmessage('number too small') else
if xx>65535 then showmessage('number too big') else
Showmessage('number accepted');
except
showmessage('Not a valid number');
end;
end;

commented: Thanks for help +1

BIG Thanks for replay to:
quaifp1
cao
FlamingClaw

i have solved .........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.