"is not a valid integer value" message?!

I have an edit mask on a form which allows a user to
enter information in the 999/999/999/999 format; All
i want to do is that when the user enters information
i want to restrict the range of the number they are allowed
to enter; i have tried to modify the code i have at the
moment in every way i can think of to make it work properly
 but it does not seem to work. Until the first forward slash i want to restrict the number between 0 and 600 , then between the first and second forward slash i want to restrict between 1 and 600, 2nd and 3rd forward slash 1 to 600 and the last part after the third forward slash 1 to 600 as well.

When i convert the string to an integer; it comes up with an error message saying it is not a valid integer value! I have tried using trim, trimright trimleft and
maskedit1.text:=StringReplace(maskedit1.text,'','',[rfReplaceAll]);
 
but i get the same problem each time,so for some reason it doesnt seem to convert properly , i have tried the same with onexit which works kind of but i dont like it that way.

the code is

procedure TForm1.MaskEdit1(Sender: TObject);
var

s : string;
v : integer;

begin

s := Copy(MaskEdit1.Text, 1, 3);
s := Trim(s);
v := StrToInt(s);
if (v = 0) or (v > 600) then ShowMessage('Incorrect number');

s := Copy(MaskEdit1.Text, 5, 3);
s := Trim(s);
v := StrToInt(s);
if (v < 1) or (v > 600) then ShowMessage('incorrect number');

s := Copy(MaskEdit1.Text, 7, 5);
s := Trim(s);
v := StrToInt(s);
if (v < 1) or (v > 600) then ShowMessage('Incorrect number');

s := Copy(MaskEdit1.Text, 9, 7);
s := Trim(s);
v := StrToInt(s);
if (v < 1) or (v > 600) then ShowMessage('Incorrect number');

end;
end.

help please! thanks in advance
itektasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
use a mask like (if i had it correct in mind)

000/000/000/000

forces the user to enter a digit for each 0

meikl ;-)
0
itektasAuthor Commented:
yes it does force the user to enter a number for each space but that doesnt solve the problem because i want the user to be able to leave spaces, for instance they must be able to enter

 3 / 3 /3  /  0

and when the check is performed in this case it would be correct; but for example in

0  /650/ 5/  5

should prompt the user twice for the first and second part. at the moment i still have the problem where that annoying message appears no matter what i enter! so it doesnt let me finish entering the number until the slash but gives me that message each time.
0
kretzschmarCommented:
any thought about to use four editboxes ?

well, late now here
i will look tomorrow again in this q, if it is unsolved
(will try to reproduce your problem then)

meikl ;-)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

itektasAuthor Commented:
no it needs to be all in one editmask :(
0
robert_marquardtCommented:
Please count again. Your Copy statements are wrong.
According to the mask you want to copy from index 1, 5, 9, 13 with 3 chars each time.
0
delphizedCommented:
The best way would be to make a parse of the string:

var S,S2:String;
  v:Integer;
begin
  S:=edit.text;
  //step 1
  S2:=copy(S,1,pos('/',S)-1);
  S2:=trim(S2);
  v:=inttostrdef(S2,0);
  if v .....
  delete(S,1,pos('/',S));
  //go to step 1 and repeat 4 times
0
kretzschmarCommented:
my version so far

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask;

type
  TForm1 = class(TForm)
    MaskEdit1: TMaskEdit;
    Button1: TButton;
    procedure MaskEdit1Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;


procedure TForm1.MaskEdit1Exit(Sender: TObject);
var
  sl : TstringList;
  s : string;
  i,f,v : integer;

begin
  sl := TStringList.Create;
  try
    s := maskedit1.Text;
    while s <> '' do
      sl.Add(trim(splitstr(s,'/')));
    //checks
    f := -1;
    i := 0;
    while (i < sl.Count) and (f = -1) do
    begin
      v := StrToInt(sl[i]);
      if (v < 1) or (v > 600) then
        f := i
      else
        inc(i);
    end;
    if f <> -1 then
    begin
      showmessage('Incorrect number');
      maskedit1.SetFocus;
      maskedit1.SelStart := (f*3)+1;
      maskedit1.SelLength := 1;
    end;
  finally
    sl.Free;
  end;
end;

end.

meikl ;-)
0
cerdalCommented:
how about this:

procedure TForm1.Edit1Exit(Sender: TObject);

  function AnswerOK(Value:Integer;IncZero:Boolean):Boolean;
  begin
    Result := (Value <= 600);
    if Result then
    begin
      if IncZero then
        Result := (Value >= 0)
      else
        Result := (Value > 0);
    end;
  end;

var
  j: Integer;
  AnswerNum: array[0..3] of Integer;
  ThisChar: char;
  ThisIntStr: string;
  MyStr: string;
  I: Integer;
begin
  MyStr := Edit1.Text;
  ThisIntStr := '';
  j := 0;
  for I := 1 to length(MyStr) do    // Iterate
  begin
    ThisChar := MyStr[i];
    case ThisChar of
      '/':
      begin
        AnswerNum[j] := StrToIntDef(ThisIntStr,-1);
        inc(j);
        if j>4 then
          break;
        ThisIntStr := '';
      end;
      ' ': ;
      '0'..'9':
      begin
        ThisIntStr := ThisIntStr + ThisChar;
      end;
    else
      MessageDlg('only figures, spaces and slashes, please',mtError,[mbOK],0);
      Edit1.SetFocus;
      abort;
    end;
  end;    // for

  AnswerNum[j] := StrToIntDef(ThisIntStr,-1);

  if //(j=4) and
     AnswerOK(AnswerNum[0],true) and
     AnswerOk(AnswerNum[1],false) and
     AnswerOk(AnswerNum[2],false) and
     AnswerOk(AnswerNum[3],false) then
  begin
    MessageDlg('values OK',mtInformation,[mbOK],0);
    exit;
  end;

  MessageDlg('0-600 / 1-600 / 1-600 / 1-600 , please',mtError,[mbOK],0);
  Edit1.SetFocus;
  abort;
end;

Chris.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
classmateCommented:
I seriously think this is the way to do it ;-)



const
  invalidNo = -1;
  IgnoreChars : string = '_';

function GetSeparateNumber (Input : String; index, LoVal, HiVal : integer) : integer;
// use this function to find a seperate number at any time
var
  p : integer;

begin
  For p := 1 to length (ignoreChars) do
    Input := StringReplace (Input, IgnoreChars[p], #32, [rfReplaceAll]);

  p:=1 + (index * 4);
  result := StrToIntDef (
    trim(
      copy (Input, p, 3)
    ),
    InvalidNo
  );

  if result = invalidno then
    exit
  else if (result < LoVal)
    or (result > hiVal)
  then
    result := invalidno;
end;



procedure TForm1.MaskEdit1KeyPress(Sender: TObject; var Key: Char);
var
  ed : TCustomEdit;
  s:string;
  i : integer;
begin
  If Key = #32 then begin
    ed.sellength := 1;
    ed.Perform(WM_KEYDOWN, VK_DELETE, 1);
    ed.Perform(WM_KEYDOWN, VK_RIGHT, 1);
  end;

  if Key = DateSeparator then
    exit;

  if key > #32 then begin
    ed := sender as TCustomEdit;
    s := ed.Text;
    delete (s, ed.SelStart + 1, ed.SelLength);
    insert (key, s, ed.selstart + 1);
    i := ed.selstart div 4;
    if GetSeparateNumber (s, i, byte (i>0), 600) = InvalidNo then
      Key := #0;
  end;
end;


regards
classmate
0
classmateCommented:
an exit statement could be added at the end of the firs if-block
0
itektasAuthor Commented:
ok, i have looked at your way of doing it meik1 , the only problem with your method is that if the user leaves the maskedit completely empty to move to another object on the form then i get the message " is not a valid integer value" . But if i fill each part completely then the erros are detected fine, so would there be a way of getting rid of that message do you think? i couldnt get delphized's code to work when i change it for all 4 parts, im probably doing mistake somwhere, havent looked to yours yet cerdal.

0
itektasAuthor Commented:
typo    erros =errors
0
itektasAuthor Commented:
cerdal your code works very well; just one thing tho, what line would i need to add so that if the user leaves it blank , i mean doesnt fill anything in, it doesnt prompt with the error message when they change to another object on the form? Because basiclly i have several edit fields on my form and i want the user to be able to fill them in the order they want, and they should be able to leave them blank without a message annoying them if they havent entered anything; the error messages are to check the content if they do enter data, but ideally if left blank no check should be made.
0
itektasAuthor Commented:
its ok now i have solved that myself just by adding a simple line! Thank you everyone  :)
 
0
cerdalCommented:
Glad to have been of help - this is my first "accepted answer", so it's a big occasion for me!

Chris

P.S.
I've noticed that the array of Integers isn't initialised to zeroes, so to avoid any possibility of getting random initial values accepted in error you should either initialise it yourself at the beginning of the procedure, or make it a global variable or a property of the Form, in which case I think it'll be initialised automatically.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.