Solved

Help! restricting user input in MaskEdit

Posted on 2003-12-02
16
878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
16 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9860390
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
ID: 9861525
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
ID: 9861546
v should be an integer:

var v : integer;

Geoff M.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:itektas
ID: 9861597
the errors that i get are

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

thanks
0
 

Author Comment

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

Author Comment

by:itektas
ID: 9861698
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
ID: 9861794
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
ID: 9862614
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
 

Author Comment

by:itektas
ID: 9868459
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
ID: 9877862
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
ID: 9878706
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
ID: 9881031
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
ID: 9882083
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
ID: 9882115
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
ID: 9882152
thanks for your help by the way, i have also increased the points  =)
0
 

Author Comment

by:itektas
ID: 9885163
ok solved the problem; thanks and enjoy the points

regards
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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