Cross Plugin JavaScript Project: is.js

Sometimes it's necessary to check whether certain data is true or false. Writing regular expressions for those cases is redundant and to be frank - boring. The is.js project aims to simplify the testing process. At the point of this writing there are three different 'plugins' available: Vanilla JavaScript function, jQuery plugin and a NPM module.

How it works

It's really simple to get going with each one of the before state 'plugins' and here's how:

Vanilla JS

Download is.js and include the file in your project as such:

<script src="js/is.js"></script>  

Then simply start testing for example zip codes:

// Icelandic zip code
is('112', 'zip:is');

// USA zip code
is('32044', 'zip:us');

...both return true.

jQuery Plugin

This one has received most love'in and is perhaps the most useful out of the tree. Again download the plugin and include it somewhere after the actual jQuery source:

<script src="js/jquery.min.js"></script>  
<script src="js/jquery.is.js"></script>  

Then it's just a matter of start testing:

// Works on pretty much all DOM elements...
$('.email').is('email'); // Returns boolean value...

// And basic strings!
$.is('john.doe@example.com', 'email'); // Returns true

Attention: Testing email addresses using regular expressions isn't 100% bullet proof - please test the thing at the backend as well.

NPM module

Using the module is three step process: Install, Require, Use.

1) Install

npm install isjs  

2) Require

var is = require('isjs');  

3) Use

// Clever use : returns boolean value
is('000-0000', 'phone')


// Not as clever use : also returns boolean value
is('000-0000', /\d{3}-\d{4}/)  

Keywords

cc (Credit cards)

Works for credit cards

// Example (vanilla JS)
is('0000000000000000', 'cc'); // Returns true  

datetime (Date and time)

Format: 1996-12-19T16:39:57-08:00

// Example (vanilla JS)
is('1996-12-19T16:39:57-08:00', 'datetime'); // Returns true  

isbn (ISBN)

ISBN 10

// Example (vanilla JS)
is('0-85131-041-9', 'isbn'); // Returns true  
is('0851310419', 'isbn'); // Returns true  

ISBN 13

// Example (vanilla JS)
is('978-1-56619-909-4', 'isbn'); // Returns true  
is('9781566199094', 'isbn'); // Returns true  

latlng (Latitude and longitude)

Latitude

// Example (vanilla JS)
is('64.163296', 'latlng'); // Returns true  

Longitude

// Example (vanilla JS)
is('-21.859328', 'latlng'); // Returns true  

phone (Phone numbers)

Iceland - format: ###-####

// Example (vanilla JS)
is('000-0000', 'phone'); // Returns true  
is('0000000', 'phone'); // Returns true  

zip:'two letter ISO 3166 country code' (Zip codes for countries)

Australia - format: ####

Austria - format: ####

Belgium - format: ####

Brazil - format: #####-###

Canada - format: A#A #A#

Danmark - format: ### (or) ####

Germany - format: #####

Great Britain - format: AA#A #AA (or) A#A #AA (or) A# #AA (or) A## #AA (or) AA# #AA (or) AA## #AA

Hungry - format: ####

Iceland - format: ###

Italy - format: #####

Japan - format: ###-####

Luxembourg - format: L-####

Nertherland - format: 1### AA

Polland - format: ##-###

Spain - format: [01-50]###

Sweden - format: ### ##

United States of America - format: ##### (or) #####-####

// Example (vanilla JS)
is('112', 'zip:is'); // Returns true  
is('32044', 'zip:us'); // Returns true  

Last Words

This project is ongoing and so there may very well be errors and loose ends here and there. If you want to contribute then by all means fork away at the GitHub repo.

The phone keyword will soon become phone:'two letter ISO 3166 country code' just like the zip codes are. Currently it's just validating Icelandic phone numbers.