Clean data makes me smile.
No really! When I have a finite number of brain cycles to dedicate to some process that receives user data, it makes me quite the happy guy knowing that it has been pre-scrubbed for such nasties as newline characters, the occasional ☺, or worse, the dreaded ಠ_ಠ.
Brion sent in this function which is supposed to prevent users from entering unclean text into a "thank you note" text area on a professional recognition site. Now, before bashing the function for it's curious name, I must say that it does indeed work at filtering out a bunch of characters that the original developer thought would cause an issue.
function numbersonly(myfield, e, dec) { var key; var keychar; if (window.event) key = window.event.keyCode; else if (e) key = e.which; else return true; keychar = String.fromCharCode(key); // control keys if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) return true; // numbers else if ((("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZa" + "bcdefghijklmnopqrstuvwxyz.,-/?!$ ").indexOf(keychar) > -1)) return true; // decimal point jump else if (dec && (keychar == ".")) { myfield.form.elements[dec].focus(); return false; } else return false; }
Thank goodness everybody always runs JavaScript.