Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crystal Reports Check If String Contains Any Numerical Value

I'm using Crystal Reports and in 1 of my formulas, I'd like to check if a string contains any numerical value or not. Examples are shown below...

"Chris(12)" Returns True
"123"       Returns True
"Pot"       Returns False
"John0"     Returns True

I've already achieved what I want using the INSTR() function. I did it like this...

if INSTR(string,"0") <> 0 or INSTR(string,"1") <> 0 or INSTR(string,"2") <> 0 ... then
   True
else
   False

I'd just like to know if there's any shorter or more efficient code. Thank you very much.

like image 951
chris_techno25 Avatar asked Jan 22 '26 14:01

chris_techno25


1 Answers

Create a custom function named ContainsNumber:

Function (Stringvar text)

  Local Booleanvar found := False;
  Local Numbervar i;

  For i := 1 To Len(text) Do (

    If IsNumeric(Mid(text, i, 1)) Then (
      found := True;
      Exit For
    )

  );

  found;

Use in formula field:

// FALSE
ContainsNumber ("ABC")

// TRUE
ContainsNumber ("ABC123")
like image 88
craig Avatar answered Jan 25 '26 19:01

craig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!