Qbasicnews.com

Full Version: Function to validate string for numeric with decimal point
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
ok i think i get it know. like this?


Code:
FUNCTION validate% (userinput$, decmax%)
validate% = -1
FOR i = 1 TO LEN(userinput$)
IF MID$(userinput$, i, 1) = "." THEN
  dec% = 1
ELSE
  IF dec% = 1 THEN decamount% = decamount% + 1
  IF ASC(MID$(userinput$, i, 1)) > 57 OR ASC(MID$(userinput$, i, 1)) < 48 THEN validate% = 0: EXIT FUNCTION
END IF
IF decamount% > decmax% THEN validate% = 0: EXIT FUNCTION
NEXT i
END FUNCTION
*murders nathan and whitetiger*

EDIT:
Quote:Works pretty good, except for the following:
* There's a PRINT in the middle of your code which probably was for debugging purposes.
Not anymore..

Quote:* The function allows nulls.
* The function allows leading blanks.
* The function allows trailing blanks.
Wrong!
Quote:AGAMEMNUS,

Works pretty good, except for the following:
* There's a PRINT in the middle of your code which probably was for debugging purposes.
* The function allows nulls.
* The function allows leading blanks.
* The function allows trailing blanks.

The decimal point logic works fine --- nice work.
*****

Sorry Aga, I addressed the post to the wrong name.
*****
see message at top
what about mine?
NATHAN,

Yours works fine except it allows leading and trailing spaces, and a very subtle point, when zero decimals are specified, it permits one trailing decimal point. Yes, arithmentically they are the same, but if I store this field into a file, I don't want the extraneous decimal point to occupy a byte on the end.
*****
WHITETIGER,

Works pretty good, except:
* allows leading and trailing spaces.
* allows multiple decimal pointss, like 12.34.56
* when zero decimals are specified, it allows one trailing decimal point.
*****
Quote:NATHAN,

Yours works fine except it allows leading and trailing spaces, and a very subtle point, when zero decimals are specified, it permits one trailing decimal point. Yes, arithmentically they are the same, but if I store this field into a file, I don't want the extraneous decimal point to occupy a byte on the end.
*****

Okay, I missed the zero passed as a parameter. My fault. Fixed:

Code:
FUNCTION validate% (stg$, maxDec%)
   if stg$="" then validate%=0:exit function
   hasDots% = 0
   FOR i% = 1 TO LEN(stg$)
      m$ = MID$(stg$, i%, 1)
      IF m$ = "." THEN
         ' We just want a decimal dot
         IF hasDots% THEN validate% = 0:  EXIT FUNCTION
         hasDots% = -1
      ELSEIF m$ < "0" OR m$ > "9" THEN
         ' We just want numeric chars
         validate% = 0: EXIT FUNCTION
      END IF
   NEXT i%
   ' Check the # of decimal spaces
   IF hasDots% THEN
      IF maxDec%=0 then validate%=0: EXIT FUNCTION
      i% = INSTR(stg$, ".")
      IF LEN(stg$) - i% > maxDec% THEN validate% = 0:  EXIT FUNCTION
   END IF
   validate% = -1
END FUNCTION

Anyhow, it doesn't allow leading/trailing spaces. The first FOR just exits when it finds a non ".", "0"-"9" character, which includes spaces. I've tested it.
...
AGAMEMNUS,

I checked your program again. It allows null, leading and trailing spaces.

I do the following:
INPUT ¨"Enter the field"; A$
*****
Pages: 1 2 3 4 5 6