• (cs)

    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.

  • 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.")

  • (cs) in reply to Andy Goth
    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!

  • Pat (unregistered)

    Time to go enter $-%

  • Kelly (unregistered) in reply to Pat
    Pat:
    Time to go enter $-%

    Hee

  • (cs)

    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.

  • Spoe (unregistered)

    ^$-?[:digit:]*.[:digit:]{2}$ anyone?

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

  • dkf (unregistered) in reply to Spoe
    Spoe:
    What language doesn't has a regex library available these days?
    Brainf*ck.
  • (cs) in reply to IceFreak2000
    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?

  • 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.

  • (cs) in reply to lovedumplingx
    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.

  • tryWhitespace (unregistered) in reply to dkf
  • remz (unregistered)

    that snippet is more or less what we were told to do in first year of computer sciences...

  • Calli Arcale (unregistered) in reply to TheD

    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.

  • (cs)

    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".

  • 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.

  • 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?)

  • joule (unregistered)

    The thing is, it doesn't accomplish it's objective very well. Unless $-. is a valid currency.

  • Bob (unregistered) in reply to Spoe
    Spoe:
    ^\$-?[:digit:]*\.[:digit:]{2}$ anyone?

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

    The readable, maintainable ones.

  • Chris Barts (unregistered) in reply to BiggerWTF
    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.

  • Mean Mr. Mustard (unregistered) in reply to cconroy

    No, you ninny! It means "Rolling On the Floor, Laughing Ass My Off"!

  • 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

  • nickf (unregistered) in reply to Spoe
    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).

  • (cs) in reply to Mean Mr. Mustard
    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.
  • nickf (unregistered) in reply to 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.
  • dpm (unregistered) in reply to dkf

    You should write one.

  • John Doe (unregistered) in reply to Spoe
    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.

  • (cs)

    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?

  • Val (unregistered) in reply to Bob

    So Brainf*ck is a readable, maintainable language, isn't it?

  • (cs) in reply to Spoe
    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.

  • (cs) in reply to joule

    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.

  • aaron (unregistered) in reply to Demaestro
    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.

  • 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 "^.^,^-^".

  • Dividius (unregistered) in reply to nickf
    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?

  • Nobody (all that) important (unregistered) in reply to webrunner
    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 ..

  • Nobody (all that) important (unregistered) in reply to Nobody (all that) important

    hehe ... ok .. never mind ... did my own WTF ... missed the comment: "//" of that return .. sigh

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

  • (cs) in reply to webrunner
    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.

  • Mike (unregistered) in reply to BiggerWTF

    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.

  • TraumaPony (unregistered) in reply to Coincoin

    YES, THIS CODE SNIPPET IS WORSE THAN FAILURE.

    IT'S -WORSE- THAN -FAILURE-.

    I hold a grudge indefinitely.

    Anonymous does not forgive.

  • TraumaPony (unregistered) in reply to TraumaPony

    And I'm aware that I have my name in the user field, by the way.

  • Flim McBoobie (unregistered) in reply to FredSaw
    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.)

  • Frost (unregistered) in reply to nickf
    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.

  • (cs) in reply to BiggerWTF
    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?

  • (cs)

    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.

  • Gabriel (unregistered) in reply to BiggerWTF
    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.

  • 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."

  • awt (unregistered) in reply to FredSaw
    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?

  • (cs)

    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...

  • (cs) in reply to Nobody (all that) important
    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? :)

  • 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.

Leave a comment on “A Very Valid validInt”

Log In or post as a guest

Replying to comment #:

« Return to Article