Posts: 2,765
Threads: 138
Joined: Nov 2002
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
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 3,368
Threads: 195
Joined: Jan 2003
*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.
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.
Posts: 2,765
Threads: 138
Joined: Nov 2002
what about mine?
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 6,419
Threads: 74
Joined: Mar 2002
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.
Posts: 3,368
Threads: 195
Joined: Jan 2003
...
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.
Posts: 1,956
Threads: 65
Joined: Jun 2003
AGAMEMNUS,
I checked your program again. It allows null, leading and trailing spaces.
I do the following:
INPUT ¨"Enter the field"; A$
*****
|