itektas
asked on
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(Sen der: 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
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(Sen
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
ASKER
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(Sen der: 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;
s: string;
v:string;
but i cant compile at the moment, can you see a problem in the code? thanks
procedure TForm1.MaskEdit1Change(Sen
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;
v should be an integer:
var v : integer;
Geoff M.
var v : integer;
Geoff M.
ASKER
the errors that i get are
[Error] Unit1.pas(36): Incompatible types: 'String' and 'Integer'
thanks
[Error] Unit1.pas(36): Incompatible types: 'String' and 'Integer'
thanks
ASKER
hadnt reloaded the page so didnt see your comment, it compiles now just trying it
ASKER
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?
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.
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.
ASKER
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(Sende r: 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.
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(Sende
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.
ASKER
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
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.
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.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(Sende r: 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(Sende r: 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.
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(Sende
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
But the following doesnt work
procedure TForm1.MaskEdit1Exit(Sende
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
end;
end.
ASKER
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(Sende r: 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.
procedure TForm1.MaskEdit1Exit(Sende
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
end;
end.
ASKER
thanks for your help by the way, i have also increased the points =)
ASKER
ok solved the problem; thanks and enjoy the points
regards
regards
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.