CodeSOD: Advanced Programming Resources

This post was originally published on this site

The Daily WTF

Denilson‘s was doing some appliance shopping, and upon submitting a form, it errored out. Denilson was sure the form was correct, and did what any of us would do: pop open the developer console and check the code.

The console dumped out a huge number of errors. And to no one’s shock, the code had all sorts of home-made validation methods, like validateNumber. And, much to my surprise, they found new ways to make the simple task of validating a number horrible.

function validateNumber(value) { var digit, tam, dif; for (i=0;i<value.length;i++) { digit = value.substring(i,i+1); /* Advanced Programming Resources -> Must be improved… */ if(digit != 0) if(digit != 1) if(digit != 2) if(digit != 3) if(digit != 4) if(digit != 5) if(digit != 6) if(digit != 7) if(digit != 8) if(digit != 9) if(digit != “.”) if(digit != “-“) { return 2; } } if(value == “”) return 1;

To read the full article click on the 'post' link at the top.