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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With