Help! restricting user input in MaskEdit

I am using an Edit mask where a user can enter numbers in the following format 999/999/999/999, the user  can not remove the forward slashes so he has to enter the information around them. What  I want to do is to restrict the range of the numbers they can enter in the different parts of this,  
so that until the first forward slash they are only allowed to enter a number between 1 to 500 . Between the 1st and 2nd forward slash 0 to 600 , 2rd and 3rd 0 to 600 and after the 3rd 0 to 600 as well. So if a number which is not whitin the range is entered i want a messagedialog to popup and say " please enter a number between...."  and not allow the incorrect number to be entered. I have previously tried to put the restriction in the input mask of the editmask but it doesnt seem to work that way so i thought i would use the on change procedure. The code I have written is as follows but it does not work at all

procedure TForm1.MaskEdit1Change(Sender: TObject);
var

I : Integer;


begin

 for I:=0 to   2 do

if MaskEdit1.Text>'500' then

showmessage('Incorrect number');


 for I:=4 to   6 do

 if MaskEdit1.Text ='600' then

showmessage('Incorrect number');

for I:=8 to 10 do

if MaskEdit1.Text='600' then

showmessage('Incorrect number');

for I:=11 to 13 do

if MaskEdit1.Text='600' then

showmessage('Incorrect number');

Hope I made the question clear enough. 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.

gmayoCommented:
You need to:
1. Copy the part you want to test
2. Turn it into a number

So:
s := Copy(MaskEdit1.Text, 1, 3);
v := StrToInt(s); // No try/except as I assume the mask edit is set up to only allow digits
if (v < 1) or (v > 500) then ShowMessage('blah blah');

...next part...
s := Copy(MaskEdit1.Text, 5, 3);
v := StrToInt(s); // No try/except as I assume the mask edit is set up to only allow digits
if (v < 1) or (v > 600) then ShowMessage('blah blah');

Technically, you *can* test numbers in strings directly, but I find the above more reliable.

Geoff M.
0
itektasAuthor Commented:
Ok, this is probably going to be quiet a silly question but, i cant seem to compile it at the moment, perhaps ive missed something out in the code?   i have also tried replacing s and v by string instead of integer

s: string;
v:string;

but i cant compile at the moment, can you see a problem in the code? thanks


procedure TForm1.MaskEdit1Change(Sender: TObject);
var

s : string;
v : string;

begin

s := Copy(MaskEdit1.Text, 1, 3);
v := StrToInt(s);
if (v < 1) or (v > 500) then ShowMessage('blah blah');

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

end;
0
gmayoCommented:
v should be an integer:

var v : integer;

Geoff M.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

itektasAuthor Commented:
the errors that i get are

[Error] Unit1.pas(36): Incompatible types: 'String' and 'Integer'

thanks
0
itektasAuthor Commented:
hadnt reloaded the page so didnt see your comment, it compiles now just trying it
0
itektasAuthor Commented:
as i enter any number in the maskedit, i get an error message saying " Is not a valid integer value. So if i start by entering 9 then it says " 9 not a valid integer value" then i enter another 9 it says "99 not a valid integer value" and on the third 9 it does display the message " blah blah" but why does it keep displaying not valid message?
0
gmayoCommented:
Have a look at the string you get from Copy(). It might have underscores in it or spaces, depending on how you set up the maskedit, both of which need deleting. If it is spaces, then

s := Copy(MaskEdit1.Text, 1, 3);
s := Trim(s);
v := StrToInt(s);
if (v < 1) or (v > 500) then ShowMessage('blah blah');

Or you could wait until the entire field has been populated and *then* check the results (eg, do it in the OnExit instead).

Geoff M.
0
itektasAuthor Commented:
The on exit works but it is not a very efficient way of doing it as if i enter the wrong number in all 4 parts then as the editmask is left 4 messages appear one after the other. So the best way would be the message appearing as wrong numbers are entered; It still doesnt seem to work, im sure it must be a minor mistake somewhere that doesnt make it accept the numbers and show me that " is not a valid integer value" message appear.
But it is weird because it does it for the first part but not for the second anymore?! So if i enter something in the second part i am only prompted if its a wrong number when is in the first part i am still prompted with the error message for each entry.
The input mask i am using is  !999/999/999/999;1;
so there is no characters for the blanks; and the test input is   /   /   /

But as you said i would of thought the trim would solve the problem, the code now is

procedure TForm1.MaskEdit1Exit(Sender: TObject);
var

s : string;
v : integer;

begin

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

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



end;
end.

0
itektasAuthor Commented:
I have now posted a new question for the problem i have from here onwards; if you can still help me solve it i would appreciate if not i can give you the original 50 points i had set up the question for! thanks
0
gmayoCommented:
Well, the way to solve 4 messages coming up is to use a set of if...then. I think you may have missed my comment about the underscores (Trim doesn't remove those). For example:

s := Copy(MaskEdit1.Text, 5, 3);
s := Trim(s);
s2 := '';
for i := 1 to Length(s) do begin
  if s[i] in ['0'..'9'] then s2 := s2 + s[i];
end;
v := StrToInt(s2);
if (v < 1) or (v > 600) then ShowMessage('blah blah');

Geoff M.
0
itektasAuthor Commented:
i have tried a set of IF actually, but i dont know how to do it , i dont have delphi here at the moment but i guess something like

if

if

(v < 1) or (v > 600) or
(a < 1) or (a > 600) or
(b < 1) or (b > 600) or
(c < 1) or (c > 600)

 then ShowMessage('incorrect number');

so that if any of those 4 conditions are satisfied, meaning that if any of the 4 parts are not within the correct range one message would be displayed! but what would be correct syntax for that?
0
gmayoCommented:
You got it right already, except you only need one IF.

if (v < 1) or (v > 600) or
  (a < 1) or (a > 600) or
  (b < 1) or (b > 600) or
  (c < 1) or (c > 600)
 then ShowMessage('incorrect number');

I was actually thinking along the lines of:

if (v < 1) or (v > 600) then begin
  ShowMessage('Part one wrong');
end else if (a < 1) or (a > 600) then begin
  ShowMessage('Part 2 wrong');
end else if (b < 1) or (b > 600) then begin
  ShowMessage('Part 3 wrong');
end else if (c < 1) or (c > 600) then begin
  ShowMessage('Part 4 wrong');
end else begin
   // It's all valid
end;

Geoff M.
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
itektasAuthor Commented:
just spent the lat 3 hours trying everything i can think of to complete this but no way does is ever work like i want it! its really annoying i dont understand where the problem is , ok the following code works perfect, but as soon as i change it for all the parts, then nothing works! the error message keeps poping up, it seems like the positions where it tries to take the numbers arent correct but i dont understand why because they make complete sense as i try to take numbers around the slashes /
so im trying to read from 1 to 3 then a slash which is 4 then 5 to 7 then slash which is 8 then 9 to 11 and so on... but it returns invalid integer message again because it reads something else! Have a look please! I hope we are almost there

procedure TForm1.MaskEdit1Exit(Sender: TObject);


var
a:string;
b:integer;
begin

if maskedit1.text='   /  /   /   ' then

exit;
a := Copy(MaskEdit1.Text, 1, 3);
a := Trim(a);
b := StrToInt(a);

if
    ( b< 1) or (b > 600)
   then ShowMessage(MaskEdit1.Text +' Is not a valid number');


But the following doesnt work

procedure TForm1.MaskEdit1Exit(Sender: TObject);

var
a:string;
c:string;
e:string;
g: string;

b:integer;
d:integer;
f:integer;
j:integer;

begin

if maskedit1.text='   /   /   /   ' then

exit;
a := Copy(MaskEdit1.Text, 1, 3);
a := Trim(a);
b := StrToInt(a);

c := Copy(MaskEdit1.Text, 5, 7);
c := Trim(c);
d := StrToInt(c);

e := Copy(MaskEdit1.Text, 9, 11);
e:= Trim(e);
f := StrToInt(e);

g := Copy(MaskEdit1.Text, 13, 15);
g := Trim(g);
j := StrToInt(g);

if
    ( b< 1) or (b > 600)or
    (d < 1) or (d > 600)  or
   (f < 1) or (f > 600)    or
   (j < 1) or (j > 600)

   then ShowMessage(MaskEdit1.Text +' Is not a  valid entry');

end;
end.
0
itektasAuthor Commented:
I also have the following which almost works perfect! but doesnt because it is the same thing as the one i just mentioned but without using strings, directly comparing numbers, but this isnt working as if i enter a 9 , for some reason it assumes that it is more then 600!  so if i enter 9 / 9 /9/ 9 it says it is wrong when is its fine!  

procedure TForm1.MaskEdit1Exit(Sender: TObject);


var
a:string;
c:string;
e:string;
g: string;





begin

if maskedit1.text='   /   /   /   ' then

exit;
a := Copy(MaskEdit1.Text, 1, 3);
a := Trim(a);


c := Copy(MaskEdit1.Text, 5, 7);
c := Trim(c);


e := Copy(MaskEdit1.Text, 9, 11);
e:= Trim(e);


g := Copy(MaskEdit1.Text, 13, 15);
g := Trim(g);


if
    (a < '1') or (a > '600')or
    (c < '1') or (c > '600')  or
   (e < '1') or (e > '600')    or
   (g < '1') or (g > '600')

   then ShowMessage(MaskEdit1.Text +' Is not a  valid entry');

end;
end.
0
itektasAuthor Commented:
thanks for your help by the way, i have also increased the points  =)
0
itektasAuthor Commented:
ok solved the problem; thanks and enjoy the points

regards
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.