Comment On A Very Valid validInt

User-friendly data-validation is important. Rarely is a simple, red asterisk next to a form field enough to indicate what’s wrong with the input. Is it a required field? Is it too high? Is it not allowed? Generally, it’s a good idea to indicate what’s wrong with the form and how to fix it. For example, a good message for a poorly formatted currency field might be “must be formatted like currency (e.g. $9999.99).” [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: A Very Valid validInt

2007-10-03 09:24 • by Coincoin
It's always funny when a program goes through all the trouble of detecting an "error" when it could just deduce the answer with same or simpler logic.


Enter phone number : 6355551192

You haven't put hyphens at 4th and 8th position, stupid!


I mean, if it was a human being talking like this, we would probably smack him in the head.

Re: A Very Valid validInt

2007-10-03 09:27 • by Andy Goth (unregistered)
return("You must express the magnitude of the dollar amount using modern, Arabic-derived numerals formatted according to base-10 (decimal) convention, most significant digit first.")

Re: A Very Valid validInt

2007-10-03 09:28 • by IceFreak2000
155727 in reply to 155726
Andy Goth:
return("You must express the magnitude of the dollar amount using modern, Arabic-derived numerals formatted according to base-10 (decimal) convention, most significant digit first.")


ROFLAMO!

Re: A Very Valid validInt

2007-10-03 09:32 • by Pat (unregistered)
Time to go enter $-%

Re: A Very Valid validInt

2007-10-03 09:34 • by Kelly (unregistered)
155730 in reply to 155729
Pat:
Time to go enter $-%


Hee

Re: A Very Valid validInt

2007-10-03 09:39 • by lovedumplingx
I wonder if this is really a WTF and not the request from some crazy client. I had a project recently where the client was not satisfied with generic error messages and we needed to get much more in depth with what errors were being generated.

Re: A Very Valid validInt

2007-10-03 09:45 • by Spoe (unregistered)
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

What language doesn't has a regex library available these days?

Re: A Very Valid validInt

2007-10-03 09:49 • by dkf (unregistered)
155735 in reply to 155734
Spoe:
What language doesn't has a regex library available these days?
Brainf*ck.

Re: A Very Valid validInt

2007-10-03 09:53 • by cconroy
155737 in reply to 155727
IceFreak2000:
Andy Goth:
return("You must express the magnitude of the dollar amount using modern, Arabic-derived numerals formatted according to base-10 (decimal) convention, most significant digit first.")


ROFLAMO!


Rolling On Floor, Lusting After Marie Osmond?

Re: A Very Valid validInt

2007-10-03 09:54 • by BiggerWTF (unregistered)
The real WTF is the name of the function - validInt(). The function does not valid integers, it validates currency. Currency is mostly never integer based, but decimal based, even if you want to ignore formatting (which this function does not).

This is first grade programming at it's best. You should allow your users to enter data in flexible formats and your code should adjust.

Re: A Very Valid validInt

2007-10-03 09:57 • by TheD
155739 in reply to 155731
lovedumplingx:
I wonder if this is really a WTF and not the request from some crazy client. I had a project recently where the client was not satisfied with generic error messages and we needed to get much more in depth with what errors were being generated.


Agreed. This looks more like a case of runaway requirements more than an overzealous coder.

Re: A Very Valid validInt

2007-10-03 10:01 • by tryWhitespace (unregistered)
155740 in reply to 155735

Re: A Very Valid validInt

2007-10-03 10:02 • by remz (unregistered)
that snippet is more or less what we were told to do in first year of computer sciences...

Re: A Very Valid validInt

2007-10-03 10:09 • by Calli Arcale (unregistered)
155742 in reply to 155739
Whether the WTF originated with the coder or the client, it's still a WTF. It's just not necessarily becuase the coder was an anal-retentive idiot. Perhaps he even avoided a regex because it made him feel better to express his frustration through a function like this one.

But it's still a WTF.

Re: A Very Valid validInt

2007-10-03 10:16 • by FredSaw
There comes a time when you have to force your user to grow up and accept some responsibility. Regex validation is needed here, with a single error message: "Please re-enter the amount using this format: $9999.99".

Re: A Very Valid validInt

2007-10-03 10:18 • by Mike (unregistered)
I guess I will be the first one to say that I don't think this is a WTF. Just because there may be an easier way to accomplish the validation doesn't make this a WTF. Is it overly verbose? Maybe...but even that is open to opinion.

If I saw this in code I was maintaining I wouldn't say "WTF!?!", I would probably just change it to use a regex or something. Or since it appears to accomplish what it set out to accomplish I might just leave it alone.

Re: A Very Valid validInt

2007-10-03 10:23 • by SK (unregistered)
While this might not be the best way to actually implement the input validation, wrt the messages, I think you underestimate how obtuse end users can be.

captcha: tastey (spellcheck, anyone?)

Re: A Very Valid validInt

2007-10-03 10:26 • by joule (unregistered)
The thing is, it doesn't accomplish it's objective very well. Unless $-. is a valid currency.

Re: A Very Valid validInt

2007-10-03 10:28 • by Bob (unregistered)
155749 in reply to 155734
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

What language doesn't has a regex library available these days?


The readable, maintainable ones.

Re: A Very Valid validInt

2007-10-03 10:29 • by Chris Barts (unregistered)
155751 in reply to 155738
BiggerWTF:
You should allow your users to enter data in flexible formats and your code should adjust.
Right. Tell that to IBM, and everyone who programs in COBOL and RPG.

CAPTCHA: 'tastey' they are not.

Re: A Very Valid validInt

2007-10-03 10:30 • by Mean Mr. Mustard (unregistered)
155752 in reply to 155737
No, you ninny! It means "Rolling On the Floor, Laughing Ass My Off"!

Re: A Very Valid validInt

2007-10-03 10:31 • by Demaestro (unregistered)
I feel funny admitted this but I have a similar piece of code that I use to validate emails.

It has messages like... "the tld must be more then 1 character."

I don't get crappy email values though.... just fake ones;)

captcha: sanitarium

Re: A Very Valid validInt

2007-10-03 10:33 • by nickf (unregistered)
155754 in reply to 155734
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

What language doesn't has a regex library available these days?

True, I immediately thought that too, but it wouldn't be able to tell you exactly what is wrong with the input. The code is pretty much WTF-free... The Real WTF(tm) is the requirement that such a level of error reporting is needed.

The only things I'd change would be to do a really simple regex right at the top, and only THEN go through all that garbage to say what's wrong if it failed the first test - with all that code, you're gonna let some cases slip through (like what people above me pointed out).

Re: A Very Valid validInt

2007-10-03 10:35 • by FredSaw
155755 in reply to 155752
Mean Mr. Mustard:
No, you ninny! It means "Rolling On the Floor, Laughing Ass My Off"!
Tongue-in-cheek humor isn't your strong suit, is it.

Re: A Very Valid validInt

2007-10-03 10:41 • by nickf (unregistered)
155757 in reply to 155754
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

not to be anal about it, buuuut:
/^\$?-?(\d+\.?\d*|\d*\.\d+)%?$/

it's not just for currency - it does all sorts of numbers (percentages, for example), hence why you can have any number of digits after the decimal.

Re: A Very Valid validInt

2007-10-03 10:44 • by dpm (unregistered)
155758 in reply to 155735
You should write one.

Re: A Very Valid validInt

2007-10-03 10:44 • by John Doe (unregistered)
155759 in reply to 155734
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

What language doesn't has a regex library available these days?

Well, the thing is that the code does use a regex at the very beginning to filter out some errand " " text. Which doesn't belong in there anyways.

Apart from the high verbosity and the wrong naming, this code is broken (does not check the input correctly in all cases) and not culture-independent.

Re: A Very Valid validInt

2007-10-03 10:44 • by webrunner
So, wait... -10.00 is valid, $10.00 is valid, $-10.00 is valid, 10.00% is valid.. what sort of field is this? It takes unitless floats, dollars, and percentages?

Re: A Very Valid validInt

2007-10-03 10:48 • by Val (unregistered)
155761 in reply to 155749
So Brainf*ck is a readable, maintainable language, isn't it?

Re: A Very Valid validInt

2007-10-03 10:51 • by Spectre
155762 in reply to 155734
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

What language doesn't has a regex library available these days?


They actually used a regex. To strip off blanks. Sigh.

Re: A Very Valid validInt

2007-10-03 10:57 • by Foosball Girl In My Dreams
155763 in reply to 155747
A project or so back I had a requirement from the end user to recalc an entire series of fields on a keystroke by keystroke basis. For example, as they entered a price of 123.45, it would recalc the entire form for: 1, 12, 123, 123.4, 123.45, with appropriate range-check errors spewing into an error-status scrolling window. Sort of rediculous, and a cpu-pig, but the user wanted it, he agreed that it might be wasteful, but was willing to pay for it, so we did it.

Re: A Very Valid validInt

2007-10-03 11:01 • by aaron (unregistered)
155764 in reply to 155753
Demaestro:
I feel funny admitted this but I have a similar piece of code that I use to validate emails.

It has messages like... "the tld must be more then 1 character."

I don't get crappy email values though.... just fake ones;)

captcha: sanitarium


i once wrote an overly complicated email validator aswell.

mine even did a reverse lookup on the domain to see if it existed, then tryed to open a connection on port 25.

Re: A Very Valid validInt

2007-10-03 11:11 • by Lingerance (unregistered)
Well, I guess tou can't enter French formatted currency (might be Quebec only, not sure) where the dollar sign is after the numbers which actually makes sense: you say ten dollars not dollar ten for 10$.

Bonus for redunant ifs and the fact that it fails to notice having a dollar percentage. Definately not "^.^,^-^".

Re: A Very Valid validInt

2007-10-03 11:19 • by Dividius (unregistered)
155767 in reply to 155757
nickf:
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

not to be anal about it, buuuut:
/^\$?-?(\d+\.?\d*|\d*\.\d+)%?$/

it's not just for currency - it does all sorts of numbers (percentages, for example), hence why you can have any number of digits after the decimal.


$-23.26% ??

Yeah, we're deploying a version of the software for the U.K., and I'm gonna need you to come in on saturday and localize this, mmmkay?

captcha: Run outta funny letters, lamo?

Re: A Very Valid validInt

2007-10-03 11:20 • by Nobody (all that) important (unregistered)
155768 in reply to 155760
webrunner:
So, wait... -10.00 is valid, $10.00 is valid, $-10.00 is valid, 10.00% is valid.. what sort of field is this? It takes unitless floats, dollars, and percentages?


Actually, $-10.00 is invalid as well:

    if (fdollar > -1 && fminus > -1)

{
//return("You cannot use both a dollar($) sign and a minus(-) sign")
}


and yet ... they follow up with this??


if (fminus > 0)
{
if (fdollar == 0 && fminus == 1)
{
if (str.length==2)
{
return("You must have a value after a $- sign")
}
}
else
{
return("You must have a minus sign in the 1st position or immediately after a $ sign")
}
}


.. ok . I'm confused ... :\

so .. are you allowed: $- or not?? seems like it's both ..

Re: A Very Valid validInt

2007-10-03 11:21 • by Nobody (all that) important (unregistered)
155769 in reply to 155768
hehe ... ok .. never mind ... did my own WTF ... missed the comment: "//" of that return .. *sigh*

(note to self: clean glasses ... )
;)

Re: A Very Valid validInt

2007-10-03 11:24 • by spxza
155771 in reply to 155760
webrunner:
So, wait... -10.00 is valid, $10.00 is valid, $-10.00 is valid, 10.00% is valid.. what sort of field is this? It takes unitless floats, dollars, and percentages?


And are any of those ints?

My brain is now sore. IHTFP. It makes me angry some times. But, some times it makes me laugh.

Re: A Very Valid validInt

2007-10-03 11:27 • by Mike (unregistered)
155772 in reply to 155738
Not only is currency rarely integer-based, but this "validInt" function actually ALLOWS a floating-point number (look at the "fdot" variable). I actually read the comments to see if anyone had noticed this. Glad I wasn't the only one.

Re: A Very Valid validInt

2007-10-03 11:32 • by TraumaPony (unregistered)
155773 in reply to 155725
YES, THIS CODE SNIPPET IS WORSE THAN FAILURE.

IT'S -WORSE- THAN -FAILURE-.


I hold a grudge indefinitely.

Anonymous does not forgive.

Re: A Very Valid validInt

2007-10-03 11:33 • by TraumaPony (unregistered)
155774 in reply to 155773
And I'm aware that I have my name in the user field, by the way.

Re: A Very Valid validInt

2007-10-03 11:33 • by Flim McBoobie (unregistered)
155775 in reply to 155743
FredSaw:
There comes a time when you have to force your user to grow up and accept some responsibility. Regex validation is needed here, with a single error message: "Please re-enter the amount using this format: $9999.99".


You must use the percent sign. You must use the minus sign. Please use them. Now.


(I'm still waiting.)

Re: A Very Valid validInt

2007-10-03 11:56 • by Frost (unregistered)
155777 in reply to 155757
nickf:
Spoe:
^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

not to be anal about it, buuuut:
/^\$?-?(\d+\.?\d*|\d*\.\d+)%?$/

it's not just for currency - it does all sorts of numbers (percentages, for example), hence why you can have any number of digits after the decimal.


Which is more important than people probably think, in general. How many flat-pack resistors do you get for a penny? The MRP package I'm working with right now supports 4 decimal places for monetary values.

Re: A Very Valid validInt

2007-10-03 12:00 • by savar
155778 in reply to 155738
BiggerWTF:
The real WTF is the name of the function - validInt(). The function does not valid integers, it validates currency. Currency is mostly never integer based, but decimal based, even if you want to ignore formatting (which this function does not).

This is first grade programming at it's best. You should allow your users to enter data in flexible formats and your code should adjust.


Agreed. My approach to validating fields like this is to strip out non-numeric characters (possibly excepting ".") and then use built-in functionality for validating that the numeric characters left form a valid number. If it doesn't, output a single error message saying something like, "This field is required, and the format should be $xxx.xx."

Anything wrong with that?

Sure, a clever user can enter something like hi100my234name567max980powers and it validates okay...but is that behavior that the developer has to prevent?

Re: A Very Valid validInt

2007-10-03 12:01 • by fzammetti
Goes to far? You apparently haven't dealt with some of the incredibly stupid users I have over the years... It would have been perfectly appropriate to have an error message that said:

"You haven't typed anything in this field, and something is required in this field. Please use the keyboard, which is likely in front of you (it'll be the thing with a bunch of squares with letters, number and symbols on it). Please ball your hand up, then extend your index (pointer) finger out, and use it, in conjunction (read: together with) an up and down movement of your arm (the one that is attached to the hand you've balled up) and try and target the squares with letters on the keyboard in a pattern that forms a word that makes sense in this field. For example, this field is labeled First Name, so an appropriate entry might be 'bill' or 'jeff', but '12345' is most likely not an appropriate response. In addition to this activity, please remember to expand and contract your diaphram, thereby getting air into your lungs and oxygen into your blood stream for distribution throughout your body. If you are still having difficulty achieving the goal of proper data entry into this field, please contact the technical support emergency hotline at 123-456-7899 (please click HERE for details on how to use the device next to you which sends and recieves sounds between yourself and another human being, commonly referred to as a telephone)."

Yes, that would be an absolutely appropriate error message, considering some of the users I've dealt with over the years. It would be even MORE appropriate accompanied by animated graphics demonstrating what to do, hand-drawn with bright Crayola-esque colors.

Re: A Very Valid validInt

2007-10-03 12:04 • by Gabriel (unregistered)
155780 in reply to 155738
BiggerWTF:
Currency is mostly never integer based, but decimal based, even if you want to ignore formatting (which this function does not).


I recall in the chapters in Patterns of Enterprise Application Architecture that dealt with currency, they explicitly advised using an integer-based storage mechanism. Treat currency as an integral number of the smallest unit (e.g., cents for US currency). This helps prevent floating point math funniness, and ensures that you don't lose a penny here or a penny there due to rounding.

It gets hairier when you have to do currency conversions, but I think I recall that you want to still store in the denominations of one of the languages.

Re: A Very Valid validInt

2007-10-03 12:12 • by Link (unregistered)
Well, the coder's heart was in the right place. This definitely could have been implemented much better, and I'm confused as to why this field seems to be able to accept so many formats, but if anything there isn't enough input validation IMO (there are still incorrect values that can get through).

My daddy always taught me: "Son, the user should be able to mash the keys with their fist and not break anything."

Re: A Very Valid validInt

2007-10-03 12:19 • by awt (unregistered)
155783 in reply to 155743
FredSaw:
There comes a time when you have to force your user to grow up and accept some responsibility. Regex validation is needed here, with a single error message: "Please re-enter the amount using this format: $9999.99".


User: But I need to enter $1234.56! What should I do?

Re: A Very Valid validInt

2007-10-03 12:20 • by vt_mruhlin
Why is it OK for a ValidInt to have a decimal point? I guess if he checked that all digits to the right of it were 0, but he's not doing that...

Re: A Very Valid validInt

2007-10-03 12:21 • by Cloaked User
155785 in reply to 155768
Nobody (all that) important:


Actually, $-10.00 is invalid as well:

    if (fdollar > -1 && fminus > -1)

{
//return("You cannot use both a dollar($) sign and a minus(-) sign")
}



You did notice that that return statement is commented out, right? :)

Re: A Very Valid validInt

2007-10-03 12:22 • by foo (unregistered)
This is QA driven code. The first validation was very simple and then snowballed as the QA department tried every goofy combination under the sun and insisted on a specific, new error message for each case.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment