Basic Validator

2009-01-08

To kick off the new year, I’m releasing Basic Validator, a simple data checker that uses the Mootools library. As I wrote on the readme file:

Basic Validator takes a minimalist approach towards data validation: it does not include graphical alerts or nifty UI options, nor is it a full-fledged form validation library. Instead, Basic Validator focuses on the essential feature of any validator: the checking of small chunks of data.

Basic Validator can be used to check small bits of data for those quick validation checks. It can also be used as a starting point for creating your own custom validation library or functions.

Another Itch Scratched

Like many other scripts, Basic Validator is a scratcher for my own itch–an itch that involves checking small strings against regular expressions, usually for custom validation functions.

Many validation libraries do have the same capabilities as Basic Validator, but they tend to be too UI based or form-validation based. The problem is that sometimes, I need to validate data without resorting to forms or I need handle validation with my own logic for alerting the user of any input errors. If I’m using a big UI or form-based validation library, I’ll need to hack my way just to see if a string is alphanumeric or a valid email.

The simple approach of course would be to simply use regular expressions to check the data, and that’s exactly what I did with Basic Validator. But instead of littering around multiple calls to RegExp:test(), I decided to write Basic Validator to make data checking a little more DRY.

So instead of doing something like:

// Check if the data is an email address: if (/^[a-z0-9_+.-]+@([a-z0-9-]+.)+[a-z0-9]{2,4}$/.test(data)) { /* do something */ }

I could simply use Basic Validator’s methods:

// Same as code above if (Validator.test(data, "email")) { /* do something */ }

It certainly makes for cleaner code. And with Basic Validator’s magic “isDataType” methods, I could do something like:

// Using the Validator object: Validator.isEmail(data);

Clean and epic.

Some Goodies Too!

The Basic Validator includes the following “prebuilt” datatypes:

Aside from these datatypes, you’ll also be able to add your own datatypes, in case you need more and Basic Validator will also take care of adding new magic “isDataType” methods for you:

// Add a new "date" datatype that checks // if the value is in the format "dd/mm/yyyy" Validator.addType("zipCode", /[0-9]{5}([- /]?[0-9]{4})?/); // You'll then be able to do Validator.test(myZipCode, "zipCode"); Validator.isZipCode(myZipCode);

And because Basic Validator uses the Mootools library, I’ve also added a few extensions to the native String and Element types, so you’ll be able to do some magic:

var myString = "The quick brown fox died."; var myTextArea = $("my-text-area"); // New validate method.. myString.validate("alphaNum"); myTextArea.validate("email"); // Magic "isDataType" shortcuts!!.. myString.isAlphaNum(); myTextArea.isEmail();

Simple, right? Yep, that’s the goal exactly.

Basic Validate Your Data

Basic Validator is now available on GitHub, where you can find the complete source and documentation. Like Mootools, Basic Validator is released under an MIT-style license. If you have questions, comments or suggestions, feel free to contact me.