Posts: 1,956
Threads: 65
Joined: Jun 2003
Write a bulletproof function to validate a user input string that must be:
* Strictly numeric, unsigned, no leading/trailing/embedded blanks.
* But may contain a decimal point. The number of decimal places that the number can contain is a specification for the input field.
* Cannot be null, obviously.
Example: FUNCTION NUMDECIMAL (userinput$, numdecimals)
Where:
userinput$ is the string input by the user.
numdecimals is a value of 0 to n, where n is the maximum number of decimal places allowed for this field. Numdecimals may be zero.
The NUMDECIMAL function returns -1 if valid field, or 0 if invalid.
Remember that users are likely to input all kinds of garbage, including numbers with 2 decimal points, like 123.45.67
P.S. Your posted programs will be tested.
*****
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: 2,765
Threads: 138
Joined: Nov 2002
can you exzplain the challenge more clearly?
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
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.
*****
Posts: 2,765
Threads: 138
Joined: Nov 2002
by blanks do you mean spaces?
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
Mine is better, but it has a print statement, too. Err..
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
can someone answer me? am i alone?
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
WhiteTiger,
Many times you have programs which need to get input data from the user, and in many of these cases the data is supposed to be numeric and may or may not have a decimal point.
When you have a requirement for such a numeric input field, your program needs to check it to make sure it's valid. You can't just accept whatever garbage the user keys in. An example would be a field that contains the price of an item. Let's say that the price can have 2 decimal places. Then the following would be valid: 123.45 59.95 149 etc., but you could not allow -123 nor 59.995.
Get it.
So, the function does all the dirty work of validating the input data. There might be additional checking that you might do in addition to using the function, like not allowing a price less than 1.00 or greater that 9999.99, etc.
You've heard the famous term "Garbage in, garbage out". Well if garbage gets into your program or it's files, then the fault is with your program.
What else don't you understand about the specs of the function?
YES, BLANKS ARE SPACES.
*****
Posts: 3,368
Threads: 195
Joined: Jan 2003
you still didn't answer me.
i win, though, right? :king:
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: 6,419
Threads: 74
Joined: Mar 2002
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
i% = INSTR(stg$, ".")
IF LEN(stg$) - i% > maxDec% THEN validate% = 0: EXIT FUNCTION
END IF
validate% = -1
END FUNCTION