1
votes

I'm new to Pascal and FastReport. This question can probably be answered without knowledge of FastReport. Pascal is Delphi. FastReport4. Edit: I am using pascal script.

I have a text box accepting an 8 character string as input. Each character should be numeric. I'm attempting to validate each character as numeric. I've tried using the val function...

Procedure Val(S : String; var R: Real; Code : Integer);
  begin
  end;

procedure thisinputOnChange(Sender: TfrxComponent);
    var
      S     : String;
      error : Integer;
      R     : Real;    
   begin

    S := thisinput.lines.text; 
   Val (S, R, error);        
     If error > 0 then
   Button2.enabled := False;       
  end;

I got this code online. The explanation says that the function will return an error with a code greater than zero if the character cannot be converted to an integer. Is that explanation correct? Am I misinterpreting?

Right now I am trying to set a button's enabled property to false if the validation fails. I might change that to a message. For now, I would like to get it to work by setting the button property.

I'm not sure if I should be using the onChange event or another event. I'm also not sure if I need to send the input to the val function in a loop. Like I said, I'm just learning how to use this function.

I am able to validate the length. This code works...

  procedure thisinputOnChange(Sender: TfrxComponent);

  begin

     if length(thisinput.lines.text) = 8 then          
        Button2.enabled := True;

  end;  

Any suggestions? Should I use the val function or something else? Let me know if I need to provide more info. I might not be able to check back until later, though. Thanks for any help.

2
Delphi or Lazarus? Also, Val is a runtime library function (in the System unit in both Delphi and Lazarus; it's not something you implement yourself. (Your implementation does nothing at all, BTW.) Shouldn't you be validating the data before it ever gets to FastReport? FR is a reporting engine; I'm not sure why you'd expect to be using it to get input or trying to validate things there. - Ken White
It is Delphi. I just edited my post to include that. The user inputs the data in a textbox. That data is used to query a database for a report. It cannot be validated before it gets to FastReport. Going by your response, I cannot use the val function for this. Thanks for letting me know that. I'll have to find another way. - user2525015
Your method looks odd: procedure thisinputOnChange(Sender: TfrxComponent);. Is that a form method, ie should it really be procedure **TForm1.**thisinputOnChange(Sender: TfrxComponent);? Can you show your full/real code please? The mixup with Delphi or Pascal is confusing too - are you using Delphi (and which version) or Turbo Pascal or Lazarus / Free Pascal? Finally, why are you defining your own empty version of Val? As Ken says it won't do anything! You should use the real Val ("Going by your response, I cannot use the Val function" is not what Ken meant.) - David
I should have made it clear that I know that the code I posted is incomplete. I thought I had to add some code to the val procedure and then call it. I just found out that we are using pascal script. I don't believe I can use val at all with that. - user2525015
I just updated my original post to indicate I am using pascal script. That changes things as far as what I can use. Thanks to all for the responses. - user2525015

2 Answers

3
votes

You didn't specify the Delphi version. Since Delphi 2009 you can set the NumbersOnly property of a TEdit to restrict the input to digits.

3
votes

procedure System.Val(S: String; var V; var Code: Integer); is an intrinsic procedure. You don't need to define it yourself.

You can use it to validate your string as an integer.

var
  myInt,error : Integer;
...
Val(s,myInt,error);
Button2.Enabled := (error = 0); // ok if error is zero

Should the string be invalid, error points to the first invalid character in the string.


As an option, you could also use

function SysUtils.TryStrToInt(const S: string; out Value: Integer): Boolean;

Button2.Enabled := TryStrToInt(s,myInt); // ok if true

Edit: An example of using Val() with pascal script can be found here: Pascal script examples.

procedure MyVal(const s: string; var n, z: Integer);
begin
  Val(s, n, z);
end;

Register the procedure in the OnCompile method when registering your scripts:

Sender.AddFunction(@MyVal, 'procedure Val(const s: string; var n, z: Integer)');

By looking into the document FastScript 1.9 Scripting library there is an integer validation function

function ValidInt(cInt: String): Boolean

To get access to this function, follow guidlines in the document (P21):

"To get an access to these functions, pass the fsGlobalUnit reference to the TfsScript.Parent property."

Note: I could not add a link to the document, but a quick search on google should get you there.