Function to validate string for numeric with decimal point - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Function to validate string for numeric with decimal point (/thread-1434.html) |
Function to validate string for numeric with decimal point - Moneo - 07-09-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. ***** Function to validate string for numeric with decimal point - Agamemnus - 07-09-2003 .. Function to validate string for numeric with decimal point - whitetiger0990 - 07-09-2003 can you exzplain the challenge more clearly? Function to validate string for numeric with decimal point - Moneo - 07-09-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. ***** Function to validate string for numeric with decimal point - whitetiger0990 - 07-09-2003 by blanks do you mean spaces? Function to validate string for numeric with decimal point - Agamemnus - 07-09-2003 Mine is better, but it has a print statement, too. Err.. Function to validate string for numeric with decimal point - whitetiger0990 - 07-09-2003 can someone answer me? am i alone? Function to validate string for numeric with decimal point - Moneo - 07-09-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. ***** Function to validate string for numeric with decimal point - Agamemnus - 07-09-2003 you still didn't answer me. i win, though, right? :king: Function to validate string for numeric with decimal point - na_th_an - 07-09-2003 Code: FUNCTION validate% (stg$, maxDec%) |