String trim function in javascript


The trim() function in javascript removes white space from both sides of a string.

Example:




Code Sample:

<html> <head> <title>String trim function in JavaScript</title> <script language="Javascript" type="text/javascript"> String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); }; function trimval(t) { try { document.getElementById("txtout").value = t.value.trim(); } catch (err) { alert(err.Description); } } </script> </head> <body> <table align="center"> <tr> <td> <input type="text" onkeyup="trimval(this);" /> </td> </tr> <tr> <td> <input type="text" id="txtout" /> </td> </tr> </table> </body> </html>