Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object expected with trim() 2

Status
Not open for further replies.

clanm

Programmer
Joined
Dec 26, 2005
Messages
237
Location
US
I get an object is expected on the trim() line below:

function verify()
{
if (trim(document.getElementById("TextBox1").value) !== "")
{
processI();
}
else
{
alert("WBS Required!");
return false;
}
}

If I take out the trim(), then it works.
 
If I take out the trim(), then it works.
That's because trim() is not a pre-defined function in javascript. However, it's an easy 1-liner to set up our own custom method:
Code:
[!]String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g, "");};[/!]

function verify()
 {
 if ((document.getElementById("TextBox1").value)[!].trim()[/!] !== "")
 {
  processI();
 }
 else
 {
 alert("WBS Required!");
 return false;
 }
 }

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
kaht,

Thanks!

So, I'm assuming I can use this to catch if the user tries to enter a space for the textbox value?
I want to catch this and NOT allow it.

Thanks again for the help!
 
Yes, that function will remove all leading and trailing spaces from a string. If the string is made up of only spaces, it returns an empty string.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Thanks for the help!
 
Whateva, whateva!! You don't even know me! Whateva.... I hang with 12 gangs! Whateva!

But honestly, I've been using prototypes a lot lately at work. The regexp came from something that niteowl had found. My old function used to remove all trailing left spaces, then trailing right spaces with 2 regexps. niteowl's did it in 1 using the logical or operator (which I should have though of.....). I stuck it in a prototype and voila, instant trim method.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Oh, and you need to buy one of my puppies cory. They are massive chick magnets. [gorgeous]

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top