Solved

Help! restricting user input in MaskEdit

Posted on 2003-12-02
16
862 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:itektas
  • 11
  • 5
16 Comments
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
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
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
v should be an integer:

var v : integer;

Geoff M.
0
 

Author Comment

by:itektas
Comment Utility
the errors that i get are

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

thanks
0
 

Author Comment

by:itektas
Comment Utility
hadnt reloaded the page so didnt see your comment, it compiles now just trying it
0
 

Author Comment

by:itektas
Comment Utility
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
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:itektas
Comment Utility
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
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
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
 
LVL 8

Accepted Solution

by:
gmayo earned 100 total points
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
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
 

Author Comment

by:itektas
Comment Utility
thanks for your help by the way, i have also increased the points  =)
0
 

Author Comment

by:itektas
Comment Utility
ok solved the problem; thanks and enjoy the points

regards
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now