Solved

"is not a valid integer value" message?!

Posted on 2003-12-03
15
1,459 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:itektas
  • 6
  • 3
  • 2
  • +3
15 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9868552
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
 

Author Comment

by:itektas
ID: 9868627
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9868958
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
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: 9869004
no it needs to be all in one editmask :(
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9872551
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
 
LVL 5

Expert Comment

by:delphized
ID: 9872684
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9872763
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
 
LVL 1

Accepted Solution

by:
cerdal earned 100 total points
ID: 9873309
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
 
LVL 2

Expert Comment

by:classmate
ID: 9876145
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
 
LVL 2

Expert Comment

by:classmate
ID: 9876179
an exit statement could be added at the end of the firs if-block
0
 

Author Comment

by:itektas
ID: 9877263
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
 

Author Comment

by:itektas
ID: 9877282
typo    erros =errors
0
 

Author Comment

by:itektas
ID: 9878486
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
 

Author Comment

by:itektas
ID: 9878507
its ok now i have solved that myself just by adding a simple line! Thank you everyone  :)
 
0
 
LVL 1

Expert Comment

by:cerdal
ID: 9881316
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

713 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