• The Submitter (unregistered) in reply to Maurits

    No, its the second TRIM function he wrote.

    There is a TRIM  :) 

  • The Submitter (unregistered) in reply to Special

    Correct, it is cut and paste from page to page.

    It's defined about 8 times.

    Whats better, is than on many pages there is a standard include of javascript libaries.

    These libraries also has an implementation of this kind of validation (although better)

  • (cs) in reply to John Smallberries
    John Smallberries:
    Anonymous:
    Oh! Wait a second! You are running Mozila-based browser right?

    This is Mozila feature and not a standard of javascript. IE doesn't like it... [;)]

     


    Indeed! What's IE?
    (So mea culpa; empiricism takes another hit.)
    :(


    Nuthin' to feel guilty about. JavaScript is proprietary. Netscape owned JS and gave it to the Mozilla project, so whatever works in Moz is JavaScript, whether in draft or in standard. 'Twas a triumph, not a failure, of empiricism.
  • Mario (unregistered) in reply to Darrin

    Anonymous:
    What is TRIM2 and how does it differ from plain old TRIM? And why doesn't its name indicate how it's different?

    It does indicate how it's different! It must trim twice. There must be a limit to the amount of whitespace TRIM can remove, thus doing it twice is better. [:D] [:D] [:D]

     

  • (cs) in reply to Mario

    It takes a form control and modifies it willy-nilly in cruel defiance of object-orientation, but just to be a real bastard, it NEVER bothers to check whether it's a text-based control or any kind at all. It's more than happy enough to spam javascript errors all around when someone tries to pass anything else.

  • Mihai (unregistered) in reply to John Smallberries

    Several ppl complained about "error for each char",
    "window in a loop" and so on.

    Open your eyes guys!
    After the first error he does cfield.value = '';
    so cfield.value.length becomes zero and he exits the loop.

    The whole thing is still a big WTF, but don't add yours to it.

  • (cs) in reply to Mihai
    Mihai:
    Several ppl complained about "error for each char",
    "window in a loop" and so on.

    Open your eyes guys!
    After the first error he does cfield.value = '';
    so cfield.value.length becomes zero and he exits the loop.

    The whole thing is still a big WTF, but don't add yours to it.



    Open your eyes yourself! The matter has already been discussed.

    As for Mozilla, it actually implements the current ECMAscript standard, which coincides with Netscape's Javascript 1.5 specification, except it's... well... standard. IE and Opera implement their own idea of what Javascript should be. A pretty bad idea, too...

  • e (unregistered) in reply to christoofar

    christoofar:
    Every preschooler knows that one should begin sentences with "Capitalization".

    Capitalization really? Capitalization nobody ever told me that. Capitalization damn.

  • Simon (unregistered) in reply to Charles Nadolski

    -- Simon wrote: -- 2 - hardcoded character values

    • And make this whole travesty worse by having something
    • like LOWER_CASE_A used 50-odd times instead? WTF?

    No, I'm not suggesting that. Anything but that :)

    What I am suggesting is that they should have used either the methods already available for determining if a character is, in fact, a letter / number, or have used a regexp.

    But that's one of the lesser of the WTFs in this post.

    The biggest one has to be "how in gh0d's name did they pass the job interview?"

    As for the interminable arguments over the WTFness of hungarian notation - well, one can never have too many flamewars.

    Simon

  • OJ (unregistered) in reply to christoofar
    christoofar:
    The biggest (blatant) WTFery is the alert WITHIN the loop.  Bad.


    Dont you mean doing ANYTHING other than returning true if alphanumeric, and false otherwise? It shouldn't be showing alerts AT ALL (or modifying values... et cetc), that's the job of the caller :)  This person may aswell have called the function "wtfDoSomethingThatDoesntMakeSense(ctrl)".
  • Christian (unregistered)

    I'd hate to see the bfIsUnicode( cfield ) function... :-o

  • fatgeekuk (unregistered)

    Ok, not read any comments yet....

    so, here goes.

    1   Function returns no value.

    2   side effect of trimming the value within the cfield object

    3   side effect of converting the cfield to uppercase, though this could be lumped in with 2.

    4   didnt know "@@@@" was a valid alphanumeric string, but thats just me

    5   function side effect of outputing error message. surely this would be more usefully done in the caller as the caller knows the name of the field and can tell the user which one.

    6)   the whole if ( and and and and and and ) is orrible, could understand it if they where worried about non-standard character encoding... but if soo... isalnum() (if this is C) or

    string.match(/[\a\d]+/) (if its javascript, and now you don't need that charloop) would be a useful first step.

    7)   if this is used as part of the validation of form fields on an "ok" button click, it may iterate across many fields warning about each one in turn (giving horrible user impression) leaving the user with no opportunity to fix the error between each message. so the user ends up being positioned at the last one checked and found wrong and has to backtrack through each one in turn.

    8)   if the user has mistyped and included an invalid char in an otherwise valid and large string, this large string is removed and the user has to type it all again (while cursing the software)

    9)   routine accepts an empty string as alphanumeric. may be a WTF, may not...

    10)   routine will reject "european characters", but lets not go there...

    I have been guilty of (7) myself, but the investment of time required for the inhouse app to handle this problem in an elegant fashion was something I didnt have.

  • fatgeekuk (unregistered) in reply to BradC
    BradC:

     

    4) displaying an alert for EACH INVALID CHARACTER IN THE STRING!!

     

    It will only report the first one, as after that the field value is cleared and the loop will terminate.

  • Anon (unregistered) in reply to fatgeekuk

    Not 100% sure of the Javascript "API" but shouldn't substr(i,1) be substr(i,i+1)? (with something to make sure i+1 < len)?

  • (cs) in reply to Anon

    JacaScript has a string object.

    http://www.w3schools.com/js/js_obj_string.asp

    String.charAt(i)

  • (cs)

    Maybe not a WTF, but what is the user base for this application?

    "Only Alphanumeric are allowed."

    Is Aunt Thelma going to know what the hell that means?

    And for that matter, is it really a good idea to force-validate an email address anyway? I mean, it's one thing to tell the user (in so many words), "I think you've an incorrect character there, but if that's what your email really is, okay," and quite another to insist, "you vill conform to our (broken) idea of what an email address should be!" I mean, it's not like you can prevent all or even most nonfunctional email addresses a user is likely to enter. Chances are, most typos are going to be more like:

    [email protected]

    And I bet this was a required field. There goes a potential customer.

    --RA

  • Steve (unregistered)

    for ( i = 0 ; i < cfield.value.length ; i++)
      {
        var n = cfield.value.substr(i,1);
        if ( n != 'a' && n != 'b' && n != 'c' && n != 'd'
    ....      

          && n != '_' && n != '@' && n != '-' && n != '.' )
        {
          window.alert("Only Alphanumeric are allowed.\nPlease re-enter the value.");
          cfield.value = '';
          cfield.focus();
        }
        cfield.value =  cfield.value.toUpperCase();
      }
      return;
    }

    I don't work in Javascript often but I'm certain that the cfield.value.length is evaluated on the first loop and then isn't changed.  I guess you could try it in an IDE and step through and see if this is true but I know we were caught with this in massaging a string to remove certain characters and ended up with an error. 

  • The Submitter (unregistered) in reply to Rank Amateur
    Rank Amateur:

    Maybe not a WTF, but what is the user base for this application?

    "Only Alphanumeric are allowed."

    Is Aunt Thelma going to know what the hell that means?

    And for that matter, is it really a good idea to force-validate an email address anyway? I mean, it's one thing to tell the user (in so many words), "I think you've an incorrect character there, but if that's what your email really is, okay," and quite another to insist, "you vill conform to our (broken) idea of what an email address should be!" I mean, it's not like you can prevent all or even most nonfunctional email addresses a user is likely to enter. Chances are, most typos are going to be more like:

    [email protected]

    And I bet this was a required field. There goes a potential customer.

    --RA



    English was the programmers second language.
  • The Submitter (unregistered) in reply to Simon
    Anonymous:
    The biggest one has to be "how in gh0d's name did they pass the job interview?"


    He was recruited from University.

    To his credit, he delivered several major apps for the company on time and they all did work (you might not like the way they work, but they work)

    Its just a cautionary tail on having unsupervised juniors working alone and letting them develop their own 'unique' style. 

    Programming truly is an art that requires constant attention, and not just something you learn and then apply.


  • The Submitter (unregistered) in reply to Mario
    Anonymous:

    Anonymous:
    What is TRIM2 and how does it differ from plain old TRIM? And why doesn't its name indicate how it's different?

    It does indicate how it's different! It must trim twice. There must be a limit to the amount of whitespace TRIM can remove, thus doing it twice is better. [:D] [:D] [:D]

     



    function TRIM2( txt )
    {
        if ( txt == null )    return "";
        if ( txt == "" )    return "";
        for (var i = 0; i < txt.length; i++)
            if ( txt.charAt(i) != ' ' )  break;
        if ( i == txt.length )  return "";
        for (var x = txt.length - 1; x >= 0; x--)
            if ( txt.charAt(x) != ' ' )  break;
        return txt.substring(i, x+1);
    }

  • Hank Miller (unregistered) in reply to Simon
    Anonymous:
    -

    But that's one of the lesser of the WTFs in this post.


    The biggest one has to be "how in gh0d's name did they pass the job interview?"


    That one is easy.  They were the only one who looked comfortable in the suit they wore to the interview.   This is always a more important factor than any ability in places that require a dress code.   
  • (cs) in reply to Simon
    Simon:
    -- Simon wrote: -- 2 - hardcoded character values - And make this whole travesty worse by having something - like LOWER_CASE_A used 50-odd times instead? WTF? No, I'm not suggesting that. Anything but that :) What I _am_ suggesting is that they should have used either the methods already available for determining if a character is, in fact, a letter / number, or have used a regexp. Simon


    Ah, by "hardcoded" you meant checking every single letter individually.  Maybe he was trying to do some loop unrolling of some sort....
  • Evan Charlton (unregistered) in reply to christoofar
    christoofar:
    Charles Nadolski:
    John Smallberries:
    <font size="2">omg!
    reenter should not be hyphenated.</font>


    Wrong! every grammar nazi knows that it should be reënter.


    Every preschooler knows that one should begin sentences with "Capitalization". *

    *[Placement on the period of that last sentence depends on the country you live in.]


    Ah, and everyone should know not to end a sentence with a preposition :)

    "Placement on the period of that last sentence depends on the country in which you live."
  • (cs) in reply to Evan Charlton
    Anonymous:
    Ah, and everyone should know not to end a sentence with a preposition :)


    That is the sort of nonsense I will not put up with up with which I will not put.
  • (cs) in reply to Mike R

    Alot of people seem to have missed that on each iteration, not only does it convert to upper case but it converts the entire string to uppercase...WTF?!

    Also, why is he wasting so many ops with the substr method instead of just using classic indexing to grab a single character?

    window.alert("stupid"[5].toUpperCase())

  • (cs) in reply to chep
    Anonymous:

    strongarm:
    Brendan Kidwell:
    These are alphanumeric characters?

        _ @ - .

    I'm guessing the author of the code was trying to validate e-mail addresses, so the real name of the method should have been isEMailAddress()

    [...]

    If author was trying to validate e-mail address then [s]he is way off... [*-)]

    I mean ....@This@[email protected]_all.right...... (dots are included [:)])

    [email protected] [:(]

    most definitely, but would you expect the genius that wrote the gem above to be able to write logic for validating an e-mail address?

  • (cs) in reply to dubwai
    dubwai:

    strongarm:
    Brendan Kidwell:
    These are alphanumeric characters?

        _ @ - .

    I'm guessing the author of the code was trying to validate e-mail addresses, so the real name of the method should have been isEMailAddress() (of course he probably then decided I think I'll use this method for all the other fields on the page too...so I don't have to rewrite all those tests)... The answer to your question has a boatload of caveats to it. _ @ - . are not letters or digits. However, - and . can be part of a number (-1.23). So if you are looking at an individual character i.e. is this a letter or a digit the answer is definitely no. If the question is, is this character sequence a valid number? The answer is yes (unless decimals and/or negatives aren't allowed). In Java Character.isDigit('-') returns false (as does '.'). however, Double.parseDouble("-1.23") returns a double of value -1.23.

    What does this have to do with whether those chars are "alphanumeric characters"?

    My point is, from the author's code you can't tell what he was trying to do.  If his intent was to make sure the characters are alpha or numeric then he screwed up, if he was trying to determine whether the entire field is alpha or numeric he's a micrometer closer to being right.  I probably picked the wrong post in the thread to reply to (my language skills were not working very well yesterday).

  • (cs) in reply to felix

    felix:


    As for Mozilla, it actually implements the current ECMAscript standard, which coincides with Netscape's Javascript 1.5 specification, except it's... well... standard. IE and Opera implement their own idea of what Javascript should be. A pretty bad idea, too...

    From Mozilla's "standard":

    The intent is to make JavaScript 2.0 and ECMAScript Edition 4 be the same language, but JavaScript 2.0 will also include a few features for which more feedback and experience is appropriate before standardizing them.

    Yeah, standard...

  • MrMunch (unregistered)

    If only finding the WTF's were as easy as actually locating the stolen Scream...

    OTOH, solving this case will actually make you quite a bit of money.

  • (cs) in reply to Maurits
    Anonymous:
    Ah, and everyone should know not to end a sentence with a preposition :)

    Freshman to Princeton upperclassman: "Do you know where the library is at?"
    Upperclassman: "Sir, at Princeton, we don't end our sentences with a preposition."
    Freshman: "Ok. Do you know where the library is at, asshole?"

  • (cs) in reply to Jehos
    Jehos:

    felix:


    As for Mozilla, it actually implements the current ECMAscript standard, which coincides with Netscape's Javascript 1.5 specification, except it's... well... standard. IE and Opera implement their own idea of what Javascript should be. A pretty bad idea, too...

    From Mozilla's "standard":

    The intent is to make JavaScript 2.0 and ECMAScript Edition 4 be the same language, but JavaScript 2.0 will also include a few features for which more feedback and experience is appropriate before standardizing them.

    Yeah, standard...



    JavaScript 2.0 doesn't even exist yet, and its a huge topic of debate in the mozilla community.  In fact, JavaScript 2.0 and the ES4 proposal were completely thrown out the window a few years ago, and only recently have they decided to take a second look at the work done tward these proposed language additions.

    However, JavaScript 2.0 and the proposed ESR4 changes largely implement a series of notes published by the W3C they called "Spice".  Currently, a sub/super set of these changes are implemented in MS's JScript.NET.  Many of the changes create quite substantial issues (Such as two conflicting object models) that have stopped the work on JavaScript 2.0 several times over.  And in my honest (and fortunately not ONLY mine) opinion, alot of these ideas aren't that great.  They largely provide alternative syntax and semantics for existing (and actually, more flexible) functionality.  And if JS2.0 and the current ES R4 proposal are passed up, some of the mechanisms added to ecma script through these new conventions should probably be added elsewhere in the language.

    Back to the point, JavaScript 1.5 is all you can worry about because JavaScript 2.0 doesn't really exist aside from as a series of notes, discussions and proposals.  So his statement is still true.  And if you would have went to ecma-international.org and looked at the ECMA-262 specification you would see that revision 4 also does not exist.
  • chep (unregistered) in reply to Stan Rogers
    Stan Rogers:
    Anonymous:

    Is it documented in JavaScript?



    Yes. See the expanded definition of String in the JS 2.0 standard:

    http://www.mozilla.org/js/language/js20/formal/notation.html#string

    I believe it's also in the ECMAScript 4 Proposal, but that hasn't been published as an ECMA standard yet.

    JS 2.0 is not a standard yet... [:^)]

    Is it documented in JavaScript1.2?

  • Anonymous (unregistered) in reply to R.Livsey
    Anonymous:
    The form field object looks to be passed in as  'cfield' so  can actually be used for any form field.


    Then you'd need an event handler in the HTML code for each form element to trigger the validation when it loses focus. And, since focus  is given back to the form control when validation fails, once the user enters a field, he cannot leave it until he enters an acceptable value.

    It could be called by another function that only gets called when the form it submitted. Presumably, the calling function would call this function for each element that needs to be validated. But, since this function returns nothing, there is no way for the caller to know that a field failed to validate. If multiple fields are invalid, the function would just turn out multiple alerts -- none of which actually tell the user which field is invalud -- and the user would have to examine the form to see which fields had become unfilled in order to determine where the problem lies. That would especially suck if the form had many fields which were allowed to remain blank.

  • (cs) in reply to strongarm
    strongarm:
    In Java Character.isDigit('-') returns false (as does '.'). however, Double.parseDouble("-1.23") returns a double of value -1.23.


    Actually, it returns a double of value: -1.229999999999999982236431605997495353221893310546875
  • (cs) in reply to The Submitter
    Anonymous:
    Rank Amateur:

    Maybe not a WTF, but what is the user base for this application?

    "Only Alphanumeric are allowed."

    Is Aunt Thelma going to know what the hell that means?

    And for that matter, is it really a good idea to force-validate an email address anyway? I mean, it's one thing to tell the user (in so many words), "I think you've an incorrect character there, but if that's what your email really is, okay," and quite another to insist, "you vill conform to our (broken) idea of what an email address should be!" I mean, it's not like you can prevent all or even most nonfunctional email addresses a user is likely to enter. Chances are, most typos are going to be more like:

    [email protected]

    And I bet this was a required field. There goes a potential customer.

    --RA



    English was the programmers second language.

    So? Does nobody do quality control at the company? Sheesh.

  • johnb (unregistered) in reply to Darrin

    No, it doesn't return anything nor does it need to. cField is obviously a form field and as such it's acting on it directly. isAlphaNumeric is probably a bad function name. It may have started out that way, but eventually it must have evolved but not renamed.

    The rest, I can't argue with. :)

  • miss_rattles (unregistered) in reply to chep

    I say blame him...not her.  There are precious few of us around to be blamed for things like this.

  • El Se&#241;or Mart&#237;nez (unregistered)

    Ok, one thing a lot of you people are saying, which is TICKING ME OFF: the idea that one should determine whether a character is alphanumeric by an inline ASCII value comparison.

    AAAARGH, DON'T DO THAT! Get it into your heads: characters are not numbers. First of all, even if you do the wrong numeric comparison implementation, hide it away in some sort of isAlphanumeric() function—at least then it's easy to change to something internationalized.

  • David (unregistered) in reply to John Smallberries

    John Smallberries:
    <FONT size=2>omg!
    reenter should not be hyphenated.</FONT>

    WTF? It can be http://education.yahoo.com/reference/dictionary/entry/reenter

    IMNSHO, it should be, just so the double-e doesn't look funny.

  • (cs) in reply to johnb
    Anonymous:
    No, it doesn't return anything nor does it need to. cField is obviously a form field and as such it's acting on it directly. isAlphaNumeric is probably a bad function name. It may have started out that way, but eventually it must have evolved but not renamed.

    The rest, I can't argue with. :)


    The thing is that because it doesn't return true or false, it cannot properly influence the submission of the form. return; either does nothing, in which case the form ALWAYS submits, or it returns null which evaluates to false which will ALWAYS stop the form.

    So I'm assuming that clearing the field ensures that the actual validator picks up on the empty field and puts false in onsubmit.

    This function doesn't return anything useful, and only acts on the field.

    If I were this guy's mentor, I'd put a frown on my face, say "Everything is wrong.", select the code and delete it.
  • (cs) in reply to David
    Anonymous:

    John Smallberries:
    <font size="2">omg!
    reenter should not be hyphenated.</font>

    WTF? It can be http://education.yahoo.com/reference/dictionary/entry/reenter

    IMNSHO, it should be, just so the double-e doesn't look funny.


    I didn't say can not, I said should not.
    Reenter is the primary (preferred) spelling. Otherwise it would be "re-enter also reenter" not the other way 'round.

    I don't think it looks all that funny...re is such a common english prefix. If it was confusing, I'd prefer to use a diaresis rather than a hyphen to distinguish the hiatus from the diphthong.

  • (cs) in reply to chep
    Anonymous:
    Stan Rogers:
    Anonymous:

    Is it documented in JavaScript?



    Yes. See the expanded definition of String in the JS 2.0 standard:

    http://www.mozilla.org/js/language/js20/formal/notation.html#string

    I believe it's also in the ECMAScript 4 Proposal, but that hasn't been published as an ECMA standard yet.

    JS 2.0 is not a standard yet... [:^)]

    Is it documented in JavaScript1.2?



    JavaScript 2.0 is as "standard" as any JS version ever will be. JavaScript is a proprietary language belonging to Netscape Communications Corporation and currently developed and administered by Mozilla.Org. Netscape/Mozilla are free to publish any language spec they want, and JavaScript 2.0 is the current version of the language. JavaScript 1.2 was also proprietary, and has never been published as an industry standard.

    The only active language standard relating to JavaScript-like languages is ECMA-262 (ECMAScript Language Specification). The language core of ECMAScript describes most, but not all, of Netscape's JavaScript 1.2, since JS 1.2 includes not only the core language but the object bindings as well.

    Most, but not all, of the language enhancements introduced in subsequent versions of JavaScript have been submitted to ECMA for consideration in a new version of their standard. JavaScript currently implements all of ECMAScript, but it is not limited to ECMAScript.

    Microsoft's JScript also implements all of ECMA 262. Internet Explorer does not run any version of JavaScript -- it is legally prohibited from doing so. Coincidentally, though, JScript is a very similar language, and the JScript runtime can interpret text tagged as JavaScript almost as if it were native JScript.

    Most "off-brand" browsers fully support ECMAScript, so if you want to code cross-browser UIs, you should be using ECMAScript, not any flavour of JavaScript.
  • (cs)

    One thing still, this is client side data validation. One should never trust it, and if you want to do it properly, you still have to perform the data validation server side. Therefore javascript validation is not necessary in most cases.

    Just remember to save all the form posted values on error and include a good problem specific message to tell the user what went wrong there.

  • (cs) in reply to DMNT
    DMNT:
    One thing still, this is client side data validation. One should never trust it, and if you want to do it properly, you still have to perform the data validation server side. Therefore javascript validation is not necessary in most cases.

    Just remember to save all the form posted values on error and include a good problem specific message to tell the user what went wrong there.


    Client-side validation is a Good Thing, not because it can actually prevent bad data from making it to the server, but because it responds much faster than server-side validation (it should be immediate if written well, which is a great user benefit) and minimizes the number of round-trips that may be required (reducing the load on the server and minimizing bandwidth). That doesn't mean that you can get away without validating server-side, but server-side validation doesn't mean you shouldn't validate client-side. That's deliberately designing an app to be slower and require more hardware than necessary.
  • (cs) in reply to Stan Rogers

    +1.

    Server-side validation is a security feature.
    Client-side validation is a performance enhancement.  (Or a user convenience, if you want to look at it that way.)

    Both have value.

Leave a comment on “Spot The WTF: JavaScript Edition”

Log In or post as a guest

Replying to comment #:

« Return to Article