1

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.

5
  • 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. Commented Nov 12, 2013 at 21:46
  • 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. Commented Nov 12, 2013 at 21:50
  • 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.) Commented Nov 12, 2013 at 22:30
  • 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. Commented Nov 13, 2013 at 16:24
  • 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. Commented Nov 13, 2013 at 16:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm attempting to find out the Delphi version now. It's embedded in the software we are using. There is not help or about menu to specify the version. I have to find that info from a remote location.
3

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.

4 Comments

Thanks for the answer and I apologize for not providing all of the correct information. I just found out that we are using pascal script. I won't go into details, but it can be difficult to get that information where I am working. The Val function is undefined. I believe SysUtils.TryStrToInt is also unavailable. I hope this answer is useful to someone else.
@user2525015, see my added link and example how to register Val() procedure in pascal script.
Thanks, but I'm not sure if I even have access to the OnCompile method with FastReport. The report has OnRunDialogs, OnStartReport, and a few other events. I am trying to find out if I do have a way to register procedures with our setup.
Sorry, I am not familiar with FastReport utilization of scripts. But my latest addition seems to reveal a built-in function you could use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.