JavaScript Tip: Trim Strings with YUI

Any programmer worth his salt knows that when you perform even the most minimal validation on a set of input textboxes for a form, that you need to wrap some kind of trim function around the string. So many times, I see developers write the following as a validation rule:

function validate(this) {
if(this=="")
alert('Please provide a value for "this"');
}

Clearly, all the user would have to do is provide a value with either a space or line break to make the value valid. The whole point of checking the value to ensure it is not empty is to ensure that the user did not provide only non-characters as well!!! The way to accomplish this type of validation would be simply to use the common programming function known as trim. The basics is that the function will shave off any spaces at the beginning and end of a given string to ensure the string is not padded. In the case of value that has only a space, this would make the string appear as completely empty.

Well for those attempting to accomplish the above in JavaScript will be out of luck unless they write the function themselves. Now, simply searching for "JavaScript trim" provide a plethora of results that will work just fine. However, being a proponent of YUI, I prefer to use the built-in tools and functions it offers if available. Sure enough, simply searching for YUI Trim in Google yielded that the function does indeed exist and, of course, is quite easy to use:

function validate(this) {
if(YAHOO.lang.trim(this)=="")
alert('Please provide a value for "this");
}
</code>

YUI Makes Animating the DOM Easy

Despite being an Adobe fanboy, I never cared much for how the Spry framework handles things like animations. Seemed complex at the time for basic operations. At work, they have standardized on using the YUI framework. This has been great for me because I have always wanted to dive deeper into it and learn about its ins and outs. With that, I needed to do some basic fade animations for a User Interface I am working on.

I looked up the docs for the the animation package. After reading for a few minutes, I was blown away at how the YUI developers were able to execute such a powerful, customizable feature in a very simple way. First, you simply create an attributes object that contains the property you want to change (in our case it will be the opacity) and what you want to change it to:

var attributes = {
opacity: {to: 0}
}

Notice that a from attribute is not present. This is only because if from is not provided, YUI will perform the animation based on the current value of the attribute you are modifying.

Once you have your attributes object defined, animating your object is simple:

var myAnimObj = new YAHOO.util.Anim("myDiv", attributes);
myAnimObj.animate();

We instantiated a new Anim object by passing in a string of the id of the element we want to animate (note: a dom element object can also be passed), and the attributes object. Then we simply call the animate() method to play the animation.

Extremely simple and straightforward. On top of this, YUI, having the incredible event engine that it has, allows you to subscribe to events for onStart, onTween (which occurs on every frame), and onComplete providing limitless animation effects and chaining.

Be sure that when you try the examples above, that you have included the following files in your document head:

<!-- Dependencies -->
<script src="http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js" type="text/javascript"></script>

<!-- Source file -->
<script src="http://yui.yahooapis.com/2.5.1/build/animation/animation-min.js" type="text/javascript"></script>

Finally, you must also create an element that you actually want to apply the animation with an id of "myDiv" for the above examples to work.