By jthomas on August 26, 2009 at 10:54 am.
Filed under: Frameworks, jQuery
Just a quick note to bring attention to a small update made to the jQuery Popup plugin posted back in June.
The update addresses a small issue when the plugin was targeted to multiple elements or no elements at all. Updating to this version is recommended as it doesn’t change any features or break any previously existing functionality.
Head over here to read more about the plugin or go straight to the download page to pick it up.
By rgriffith on July 17, 2009 at 4:08 pm.
Filed under: Actionscript, Flex, Frameworks, Languages
In a flex app I’m building, I have two dates a user needs to put in for an event. I needed a way to make sure the end date was not before the start date. Now, I know this is pretty common sense for a user to be able to do this but you never can tell. At any rate, I needed a custom validator for this to take two dates and compare them to make sure that the end date was not before the start date when the validations occurred.
After doing some research on the Google to find ways to get me started, I came up with the following as the fruit of my labor. Please note that the comments give reference to the articles that I utilized in building this validtor.
////////////////////////////////////////////////////////////////////////////////
// Based loosley on examples found in the following places:
//
// How to Validate a Date Range in Flex:
// (Basic idea for comparing a given date to a pre-defined range. This object did not meet my needs exactly so I used this as a starting point.)
// http://www.davidortinau.com/blog/how_to_validate_a_date_range_in_flex/
//
//
// How to Compare Two Dates in ActionScript 3:
// (Shows comparing dates as Numbers)
// http://userflex.wordpress.com/2008/09/11/as3-date-compare/
//
// Flex: How do you validate 2 password fields to make sure they match?
// (Shows how to get two fields to validate, makes use of the getValueFromSource() method.)
// http://stackoverflow.com/questions/508696/flex-how-do-you-validate-2-password-fields-to-make-sure-they-match
//
// Actionscript 3 - Fastest way to parse yyyy-mm-dd hh:mm:ss to a Date object?
// (Ran into trouble on getting my dates to parse correctly given the yyyy-mm-dd format, this article pointed to a solution)
// http://stackoverflow.com/questions/3163/actionscript-3-fastest-way-to-parse-yyyy-mm-dd-hhmmss-to-a-date-object
////////////////////////////////////////////////////////////////////////////////
package com.wg.utility
{
import mx.validators.DateValidator;
import mx.validators.ValidationResult;
public class StartEndDateValidation extends DateValidator
{
private var _startDateSource:Object;
private var _startDateProperty:String;
public function StartEndDateValidation()
{
super();
}
public function set StartDateSource(value:Object): void
{
_startDateSource = value;
}
public function set StartDateProperty(value:String):void
{
_startDateProperty = value;
}
override protected function doValidation(value:Object):Array {
// Create an array to return validator results
var validatorResults:Array = new Array();
// Call base doValidation() this assures EndDate (source) is correctly validated as a date.
validatorResults = super.doValidation(value.enddate);
// Return if validation has errors.
if (validatorResults.length > 0) {
return validatorResults;
}
if (String(value).length == 0) {
return validatorResults;
}
// Use castDate to properly cast the date's text property into a Date object.
var startDate:Date = castDate(value.startdate);
var endDate:Date = castDate(value.enddate);
var startDateTimeStamp:Number = startDate.getTime();
var endDateTimeStamp:Number = endDate.getTime();
if (endDateTimeStamp < startDateTimeStamp) {
validatorResults.push(new ValidationResult(true, null, "Invalid Date Range", "End Date is before Start Date"));
return validatorResults;
}
return validatorResults;
}
override protected function getValueFromSource():Object
{
var value:Object = {};
value.enddate = super.getValueFromSource();
if (_startDateSource && _startDateProperty) {
value.startdate = _startDateSource[_startDateProperty];
}
return value;
}
private function castDate(dateString:String):Date {
if ( dateString == null )
return null;
if ( dateString.length != 10 && dateString.length != 19)
return null;
dateString = dateString.replace("-", "/");
return new Date(Date.parse( dateString ));
}
}
}
To implement this in my MXML, I needed to put in the following code. In my component declaration, I needed the following namespace so I could reference to it as a validator.
...
The fields I wanted to validate are illustrated in the following snippet below:
...
...
Make note in the above code that the format string for each field is "YYYY-MM-DD." I did this for ease of entry into my database. However, this tripped me up when trying to get an actual date object parsed in so I could use it for comparison. That is the reason for the function castDate() in the validator.
The validator array grouping I used for the component listed below shows how the validator references both the start date and the end date. The way this validator works is the validation is bound to the endDate field and the startDate is referenced by the attributes "StartDateSource" and "StartDateProperty." In addition, the endDate is also validated as a valid date by the standard DateValidator method. For validating the startDate field, I used a standard DateValidator reference.
Finally, the validation is executed (normally via a button click) with the following code:
var results:Array = Validator.validateAll(validators);
// if any of the fields were not valid
if (results.length > 0) {
return;
}
By jthomas on July 14, 2009 at 11:18 am.
Filed under: jQuery
Recently while working on a client project I was met with a situation in which I needed to do some client-side feedback to validation of a couple fields. I wanted something a little more spiced up than your typical turn-the-field-red or put-a-red-asterisk next to it.
The jQuery UI Effects library had just what I was looking for, with only one problem. In order to use the desired effect I had to include over 100 kb of additional Javascript. My app was already pretty heavy on the client side so I sought a way to achieve the effect with as little cruft as possible.
So, after a little hacking around, I present the “Mini” UI Effects plugins. They are a set of plugins that are lightweight and attempt to offer a similar experience to the jQuery UI effects. For now there are just three basic effects, “Blink”, “Wiggle” and “Bob.” They are similar to the jQuery UI “Pulsate”, “Shake” and “Bounce” effects respectively.
Please feel free to download and use them in your own projects both commercial and personal alike. They have been tested to work in IE6+, Safari and Firefox.
Check out a demo and download the jQuery Mini UI plugins here. Let me encourage you to leave feedback in the comments. We’d love to hear of any experience using them (bugs or otherwise). Enjoy.
By jthomas on June 15, 2009 at 11:37 am.
Filed under: Frameworks, jQuery
Last week, John showed a technique for enabling a popup anchor to degrade gracefully for users who do not have Javascript enabled. Inspired by his technique, I decided to throw together a jQuery plugin that implements this technique, without having the event attached inline.
It weighs in at just under 1k minified, so it shouldn’t be too tough on your page weight. Feel free to download it and use it for any kind of project.
Usage
Include the plugin as you would any jQuery plugin (where “/path/to/file” is specific to your instance).
Next, put the HTML markup in place (of course the href, target and class can be what you want):
Then, just call popup() on the desired elements:
$(".popup-link").popup();
Additionally, you can provide the desired size of the popup window like so:
$(".popup-link").popup({
width: 320,
height: 240
});
Download it here.
Update
We have posted a new version of the plugin that addresses a small bug. The download link above takes you to the demo/download page which contains the new version. Read more about it on this page.