user2525015
user2525015

Reputation: 281

Validating String Characters as Numeric w/ Pascal Script (FastReport 4)

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.

Upvotes: 1

Views: 3060

Answers (2)

LU RD
LU RD

Reputation: 34889

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.

Upvotes: 3

Uwe Raabe
Uwe Raabe

Reputation: 47694

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.

Upvotes: 3

Related Questions