You are not passing a string otherwise it would have a replace method. I hope you didnt type function trim(str) { return var.replace(blah); } instead of return str.replace.
I fixed the problem.... sorry I should have put the code on how I was calling it too.... realized I accidentally was passing the object of the form field itself rather than it's value.
make sure you are passing string to "replace" method. Had same issue and solved it by passing string. You can also make it to string using toString() method.