FoxPro functions which determine if a variable is a character string or a numeric string

Sithu picture Sithu · Dec 14, 2012 · Viewed 19.2k times · Source

I'm looking for a Visual FoxPro function which is similar to the PHP function is_numeric().
I have found this, but I could not use VARTYPE or TYPE because the variable is always a character string which contains digits only.

I found ISDIGIT() function, but the manual says that it only checks the first character.

Determines whether the leftmost character of the specified character expression is a digit (0 through 9).

  ISDIGIT(cExpression)

Parameters
cExpression

Specifies the character expression that ISDIGIT( ) tests. Any characters after the first character in cExpression are ignored.

I would create my own function using the regular expression object VBScript.RegExp

FUNCTION isNumeric( tcValue )
    LOCAL oRE
    oRE = CreateObject("VBScript.RegExp")
    oRE.Pattern = '^[0-9]+$'
    RETURN oRE.test( tcValue )      
ENDFUNC

? isNumeric( '123' )

But, is there any function provided by FoxPro for this purpose?
Am I just overlooking?

Also same for ISALHPA() which determines whether the leftmost character in a character expression is alphabetic. I want to check if the variable contain only alphabets.

Answer

Frank Perez picture Frank Perez · Dec 14, 2012

You can create your own function like this.

FUNCTION IsAllDigits
    LPARAMETERS tcSearched, tcOptionalSearch

    * tcSearched = the string of characters to test.
    * tcOptionalSearch = optional, additional characters to allow.

    LOCAL lcSearch
    m.lcSearch = "01234567989" + IIF(VARTYPE(m.tcOptionalSearch) = "C", m.tcOptionalSearch, "")

    LOCAL lcRemaining
    m.lcRemaining = CHRTRAN(m.tcSearched, m.lcSearch, "")

    RETURN ( LEN(m.lcRemaining) = 0 )
ENDFUNC