Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all

I am using following line code to trim the unwanted space in string on client side whatever the enduser will type into the textbox

if(document.getElementById("txtUserName").value.trim() == ""){
       ..........
       ..........
}


when this line execute by the browser on clinet side

it shows the error "Object doesn't support this property or method Error"

How to use the trim() function in javascript

Please Help me

Thanks in Advance...
Posted

XML
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
  <button>Show Trim Example</button>
<script>

$("button").click(function () {
var str = "     lots of spaces before and after     ";
alert("'" + str + "'");

str = jQuery.trim(str);
alert("'" + str + "' - no longer");
});

</script>

</body>
</html>
 
Share this answer
 
There are few methods to achieve trim using string object[^].

Declare a string, assign the textbox value to it and then use string methods to achieve trim feature. If needed, assign back the string value to textbox.

Here an example: Javascript string Trim[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900