Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to validate string for numeric with decimal point
#11
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
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#12
*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!
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#13
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.
*****
Reply
#14
see message at top
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#15
what about mine?
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#16
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.
*****
Reply
#17
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.
*****
Reply
#18
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.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#19
...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#20
AGAMEMNUS,

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

I do the following:
INPUT ¨"Enter the field"; A$
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)