home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / ISNUM.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  2KB  |  59 lines

  1. DEFINT A-Z
  2.  
  3. ' $INCLUDE: 'TRUEFALS.INC'
  4.  
  5. DECLARE FUNCTION IsNum (text$)
  6.  
  7. FUNCTION IsNum (text$)
  8. '****************************************************************************
  9. 'Returns TRUE if the text contains only numeric data, FALSE otherwise.
  10. '
  11. 'The function considers a lone minus sign (-) to be an operator, not numeric.
  12. ' The same goes for a lone decimal point.  Both characters ARE allowed if
  13. ' cohabitating with digits (would their mothers approve?).
  14. '
  15. 'Examples:  IsNum("12")   --> TRUE      IsNum("ABC") --> FALSE
  16. '           IsNum("-6")   --> TRUE      IsNum(" ")   --> FALSE
  17. '           IsNum("1.3")  --> TRUE      IsNum("-")   --> FALSE
  18. '           IsNum("")     --> FALSE     IsNum(".")   --> FALSE
  19. '
  20. '****************************************************************************
  21.  
  22. IsNum = FALSE                      'Assume the worst.
  23.  
  24. d$ = "."                           'For optimization.
  25. m$ = "-"
  26.  
  27. t$ = LTRIM$(RTRIM$(text$))         'Trim any spaces from text$.
  28.  
  29. IF t$ = "" THEN EXIT FUNCTION      'Return FALSE if there's nothing left.
  30.  
  31. IF t$ = d$ OR t$ = m$ THEN EXIT FUNCTION          'No lone symbols!
  32.  
  33. 'Check each character's ASCII code to make sure it is one of the following:
  34. 'A minus sign (45), a decimal (46), or a digit (48-57).
  35.  
  36. FOR x = 1 TO LEN(t$)
  37.      a = ASC(MID$(t$, x, 1))
  38.      IF (a < 45) OR (a > 57) OR (a = 47) THEN EXIT FUNCTION
  39. NEXT x
  40.  
  41. 'Make sure that there is not more than one minus sign and if there is one,
  42. 'that it is only in the first position.  Also make sure there is only one
  43. 'decimal point.
  44.  
  45. minus = INSTR(t$, m$)              'Find location of first occurrences.
  46. dot = INSTR(t$, d$)
  47.  
  48. IF minus > 1 THEN EXIT FUNCTION    'Allowed only in first position!
  49.  
  50. minus = minus + 1                                 'Make sure there's only one
  51. IF INSTR(minus, t$, m$) > 1 THEN EXIT FUNCTION    'of each.
  52. dot = dot + 1
  53. IF INSTR(dot, t$, d$) > 1 THEN EXIT FUNCTION
  54.  
  55. IsNum = TRUE                       'All tests must pass to reach this point.
  56.  
  57. END FUNCTION
  58.  
  59.