Posted: 8th Jul 2015 22:41
The IsNumeric function returns true if a string contains a decimal number such as 10, 5.5 or 0. It is used to validate data input from the user or external datasources in situations where it is crucial for the value to contain a basic numerical value with no scientific notations, words or empty string. The IsNumeric function is similar to the VB.NET equivalent sharing the same name.

The following strings will return false:

"199,999,999" because comma delimitation has not been implemented.

"Hat" because the string contains a non-numeric character.

"127.0.0.1" because there are more than one points in what is really an IP address.

"120 23" because no delimitation can be used; except for one decimal point or a prefixed minus sign.

"Delete From Database 'Players' Where 1 = 1" because this most certainly is not what the intended use for a piece of numeric input.

"" because this is an empty string

"." because at least one numeral is required

"25." because the point cannot be the last character in the string.

"+323" because a plus sign is not usually required for positive figures; but the use of a plus sign can be implemented if required.


The following strings will return true:

"100" because only numerals are found.

"1.32" because this is a valid floating point decimal number

" 323 " because the space characters are removed from either side of the valid input by the Matrix1 Trim$() function

".25" because this is a valid floating point number which is also a standard decimal value.


+ Code Snippet
//=======================================================
Function IsNumeric( sText$ )
	If sText$ = "" Then ExitFunction 0
  Local bNumeric as Boolean 
  sText$ = Trim$(sText$)
  Local iLen : iLen = Fast Len(sText$)
  Local c$ 
  Local iChar
  Local iDots
  Local iMinusSigns
  sText$ = Fast Lower$( sText$ )
  
  For i = 1 to iLen
    bNumeric = 1
    iChar = Asc( Mid$( sText$, i ) )
    If iChar = 46
      Inc iDots
      If i = iLen Then ExitFunction 0
      If iDots > 1 Then ExitFunction 0
    Else
      If iChar = 45
        Inc iMinusSigns
        If iMinusSigns > 1 Then ExitFunction 0
      Else
        If iChar < 48 
          bNumeric = 0
        Else
          If iChar > 57 Then bNumeric = 0
        EndIf
        If bNumeric = 0 Then ExitFunction 0        
      Endif  
    Endif
  Next i
Endfunction 1