- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
<font size="2">omg!
reenter should not be hyphenated.</font>
Admin
These are alphanumeric characters?
_ @ - .
Admin
If thats your biggest concern, I envy you
Admin
OK, I'm guessing that JavaScript already has some sort of isAlpha or isNumber function or functions that could have been used in the first place, right? (Of course, they probably don't count @,_,-, or . as alphanumeric.)
What is TRIM2 and how does it differ from plain old TRIM? And why doesn't its name indicate how it's different?
Wouldn't it have been easier to do the comparison as a range or set of ranges. Like if (n <= 'a') && (n <= 'z'), etc.? I mean, even if you have to account for EBCDIC, you can still use sets of ranges.
Why is the developer setting the variable to all uppercase after he's tested it against both upper and lower case letters? And why does he do it each time through the loop?
Um, does this function actually return anything? It doesn't look like it returns true or false, but I'm not a JavaScript programmer so maybe I'm missing it.
Also, I really like the alert message. "Only Alphanumeric are allowed." Alphanumeric what? And why did they capitalize "Alphanumeric?" Plus, it sure was courteous of them to erase the value the user entered so that they'll have to enter the entire string again.
And people wonder why, as a developer, I hate most software.
Admin
Ok...
1) Allowing non-alphanumeric characters in a routine called "IsAlphaNumeric"
2) Testing each character separately instead of the entire word with something like Regex
3) Testing each character with a huge, 66 part AND statement instead of something like comparing ASCII value ranges
4) displaying an alert for EACH INVALID CHARACTER IN THE STRING!!
5) Changing it to uppercase only AFTER the comparisons. Moving that before would save you, um, at least 26 comparisons
6) Is "&&" a short-curcuiting operator in JavaScript? If not, the letter 'a' would still compare 65 more times.
hehe. I'd be curious to see his TRIM2() function. Might have some equally tasty tidbits in there.... :)
Admin
Holy smokes.
1)No regular expression, let's just check every character.
2)We'll force the textbox's value to uppercase. Every time we check a character.
3)We'll alert the user EVERY TIME we encounter an unacceptable character, even if there are 12 of them in the same text box. BREAK statements are for suckers, and users need to be taught a lesson.
4)Explicitly return from the function when the control flow can't do anything BUT.
Admin
1 - hungarian 2 - hardcoded character values 3 - _@-. are not alphanumeric 4 - empty field will be treated as alphanumeric 5 - all-space field will be treated as alphanumeric 6 - name suggests will return boolean, instead opens an alert on failure. 7 - return where none needed 8 - name suggests tests field value, instead it modifies it 9 - setting value to value uppercase inside loop 10 - unnecessary checks for lowercase characters (could move the uppercase to the start) 11 - opening window alert inside the loop
Hell. I give up. there's approximately 1 error per 0.5 characters in this. It's like shooting fish in a barrel.
Simon
Admin
I believe that once the field is set to blank, the loop will exit as i > cfield.value.length.
Admin
As a TRANSACT-SQL developer might say:
What is "TRIM"? I only know of
LTRIM: trims all spaces from the beginning of the string
and
RTRIM: trims all spaces from the end of the string
TRIM2 must thus be a user function which trims both ends of the string (hence the 2)
Admin
the alert call is no big deal, it will happen only once, becuase just after displaying the message, it helpfully clears the invalid data the user typed in.
Admin
Actually, I just realized that by setting the text box to an empty string, the loop will basically end because the loop counter will be greater than the length of the string. Thus, no multiple warnings.
Ironic that one bad practice (not getting the textbox value into a variable) saved the developer from discovering another.
Admin
Thanks everybody for noticing my error while I was typin' and testin'. Yes, I have actually popped this shit code into a web page.
Admin
Thereby keeping him from learning from his mistakes, and probably ending up typing the same thing again [:'(]
Admin
Then there's substringing instead of charAt() to add to the list.
Admin
Setting the field to blank. "You typed one wrong letter so now I'll make you retype the whole thing." What a pain in the ass.
Admin
I discovered the ebay auction in the comments of yesterday's WTF.
I found the concept interesting and was able to get immediate feedback from the seller about a split shipment, sending the ones to one place and the zeros to annother.
I blogged about it, but in the end, I use a product that has the ability to generate guids so I don't need to buy one at auction. http://weblogs.asp.net/andrewseven/archive/2005/06/23/EbayGuid.aspx
Admin
I guess regex'es are for suckers only!
The whole function could be replaced with simple check on <FONT face="Courier New" color=#000080>/[^\w@\-\.]/</FONT>
[;)]
Admin
In the VB world, we also have LTrim() and RTrim() for truncating extra spaces on the left and right side, respectively. To take care of both simultaneously, we have a function called Trim(). There are no such built-in functions for Javascript, so this guy clearly wrote his own. Except that by calling it TRIM2(), he thereby proves to us that it's one better. Maybe it washes your car as it trims off whitespace, who knows.
Admin
Here's a couple of WTFs (or, at least, design problems) nobody noticed yet:
--AC
Admin
Noone seems to have covered this wtf: The whole logic is mixed in with the page implementation. This wtfery is probably copied and pasted for every form field that needs to be validated.
Admin
JavaScript has an indexer for strings, so you can just use:
n = cfield.value[i];
Admin
Let me offer an insight into how this was written:
I enter some random text that includes leading spaces.
Oops! It gives errors 25 times.
2. I'll fix it by trimming spaces.
Oops... that helped but still annoying.
3. Hmm it only handles uppercase.
Hmm I better convert the text to uppercase.
That works! (The test string had an initial cap...)
4. Maybe inserting a return statement would help.
Nope, I tried that. It didn't cause any errors, so I'll leave it there.
5. Maybe if I blank out the text field it will stop bugging me.
Perfect! Whoops it doesn't cope with lowercase letters at the beginning of a string...
6. Added 26 more comparisons.
That's how I should have done it in the first place.
Perfect!
Admin
He passes the field in as a parameter
Admin
You might want to correct the spelling of 'speech' in your signature. Unless you meant 's peach', then you'll probably need to explain what that means.
Admin
The form field object looks to be passed in as 'cfield' so can actually be used for any form field.
Although what the 'c' stands for in the hungarianesque notation I don't know.
Admin
Cocktostoen
Admin
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.
Admin
What does this have to do with whether those chars are "alphanumeric characters"?
Admin
Granted it's the least of the problems, but I pity anyone who might have to i18nize that someday.
Admin
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] [:(]
Admin
I'm sure that this didn't stop our heroic chicken slayer from copying/pasting this function into every page that needed it.
Admin
RTFM! [8-|]
In javascript, object String does not have indexer!
VBScript does.
Admin
RTFM if you want to, but I replaced one line with the other and it worked fine.
//var n = cfield.value.substr(i,1);
n = cfield.value[i];
Admin
I got a bonus wtf:
He used "cfield.value.length" inside the for loop condition. Now mind you, it wouldn't work otherwise due to him setting the string to '' possibly, but instead of checking the string length in the for loop he should have broken out of the loop instead.
Admin
Yeah! n is always undefined now... [:)]
Admin
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... [;)]
Admin
Indeed! What's IE?
(So mea culpa; empiricism takes another hit.)
:(
Admin
Actually, if it works in Mozilla, then by definition it is standard JavaScript. It isn't ECMAScript or JScript, but it is JavaScript. It isn't cross-browser, and in a read-only action it does nothing that the cross-browser, ECMA-standard charAt() doesn't also do.
Admin
The biggest (blatant) WTFery is the alert WITHIN the loop. Bad.
Admin
Wrong! every grammar nazi knows that it should be reënter.
Admin
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.]
Admin
Is it documented in JavaScript?
Admin
Please please please, let's not start this argument again. We'll have another MSprogrammers vs Everybodyelse flame war.
And make this whole travesty worse by having something like LOWER_CASE_A used 50-odd times instead? WTF?
Admin
Ah, but a capital letter doesn't necessarily have to follow an eclamation point ;)
Admin
no, VBScript doesn't have a string indexer, at least not something like
c = s[n]
or
c = s(n)
unless you know of some other syntax that I've not been able to find - you have to do this:
c = mid(s,n,1)
Admin
I found 17 problems. Most were probably already mentioned, and some are consequences of others:
"bf"IsAlphaNumeric;
function takes a form control, when it should take a string;
function modifies the form control's value;
failure to use a regex;
substr instead of charAt;
flagrantly long "if" statement;
_, @, - and . are apparently alphanumeric;
shows a message box
... with an incorrect capital letter
... and a missing word;
modifies the control again
... forcing the user to retype the entire thing;
focuses the control;
modifies the control again, this time to uppercase
...on every iteration of the loop
...after the if statement, which could have been simplified otherwise; and
does not return true or false!
Admin
You're right!
My bad! [:$]
Admin
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.
Admin
This is where I knew my mouth was gonna be...
Admin
Correct.