Extending JavaScript to Trim


As of lately I have become, well, obsessed with the Prototype JavaScript library. There is, as there always is, heated discussions across the internet of the benefits/strengths/weaknesses of many readily-available JavaScript libraries.
Such as:

But for the time being, all those libraries are irrelevant. It's great how easy they are to use, but what fun is it if you don't know enough JavaScript to edit those libraries. Still probably very fun, but let's get our hands dirty with JavaScript today!

The way these libraries work depends on extending the prebuilt JavaScript objects.

Typically when you create a function for JavaScript you'd do something like this:

trim_func function(value) {
    return value.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

And then you would use it like this:

// this would be used, probably in an input text field onBlur
trim_func(this.value);

trim_func('  hello my dear world   . ');        // returns 'hello my dear world   .' 

By extending the library either of the two following ways

// method 1
String.prototype.trim = function() {
    return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

//method 2

trim_func function(this) {
    return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}
String.prototype.trim=trim_func;

And you can use either of those two methods extending the String object like so:

this.value.trim();

Which is a beautiful thing when quickly and easily applied to a input field:

<input type="text" name="lname" id="lname" value="" title="Please enter your last name" onchange="this.value=this.value.trim();" />

So go throw one of the methods of trim to your library.js file and use it on your forms. It's great for trimming down values (so your server side script isn't the only one doing it) and also removing entries that are solely spaces to help with validation.

Check out The Prototype Property of JavaScript 1.1 on JavaScriptKit.com for more information!

See also

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
Quick and Dirty JavaScript Pop-Up Windows
Our topic today: JavaScript pop-up windows. I have a quick and dirty method I usually use to create JavaScript pop-up windows. I use it because it allows the user to still access the page if they have JavaScript turned off and it centers the window on the screen. The function accepts 3 parameters the URL, window
JavaScript Script.aculo.us Fade with a Timer
Using Scriptalicious's fade effect with a simple JavaScript timer you can send a message to your visitor . A great an effective way to increase usability and provide useful feedback after your visitor has made some action. Oh...and it looks good!
Limit Characters in a Textarea with Prototype
Limit and display the number of characters available in a text area using my little add on script to the Prototype JavaScript library. Very easy to implement, and great feedback to your form filler-outers.
Zip Code, Phone Number and Replacing Empty Fields with Really Easy Validation
While using the Really Easy Validation JavaScript validation library I've accumulated a few custom validations that for US phone numbers, US zip codes, Canadian postal codes as well as replacing empty fields or fields that don't contain a numeric value. Good stuff!
Event Observe and Event Listeners with Prototype
My most common mistake with Prototype is not defining elements before i call them with my JavaScript. Using event listeners I can ensure I don't call on an element until it, or the whole page is loaded, amongst many other reasons.
Next Post
The Tattoo Project
Previous Post
Chumby Soon?

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!