The latest and greatest notes from nicknettleton.com

Validate a credit card number in JavaScript

12 August 2007 - JavaScript

When you're writing web apps, it's good to do validation both in the browser and on the server side. Do it in the browser to provide your user with instant feedback, and on the server for real security, as all client-side validation can be tricked or circumvented by those who know how.

Validating credit card numbers is a little tricky. They all follow a special algorithm, whose name I forget. But here are the mechanics of it...

Continued »  2 comments »  Add a comment » 

Intro to microformats

08 July 2006 - Microformats

Microformats are an important  - no, very important - new idea on the web. In fact, I think they are so important, they could precipitate a leap of evolution more important than AJAX and as important as XML web services. But first, an introduction.

The focal site for microformats, microformats.org, is not clear at all on what microformats are, but here is my understanding:

Microformats build on the semantic capabilities of the web, using existing standards.

Unless you're fairly technical, that's probably meaningless. So, to explain. <h1>, <h2>, <p>, <ul> - all of these and other HTML tags are designed to tell human readers, web browsers and other HTML readers what sort of information they contain. Not what it looks like - that's what CSS is for - but how that bit of information relates to other bits on the page. Is it a heading, a paragraph or a list of things?

Continued »  19 comments »  Add a comment » 

CMYK, RGB and PHP

04 July 2006 - PHP

Brighter by the dozenA month or so ago, we were putting the finishing touches to the Lena White website, which sells OPI nail lacquer, among other things.

Lacquer is all about colour, so we wanted to give shoppers a way to quickly and visually browse all the available colours with colour swatches, rather than the more traditional product shot route. This makes particular sense because all lacquer bottles look pretty much the same, photos of them give a poor impression of the actual colour, and they take up way more screen estate than is helpful to anyone.

Continued »  6 comments »  Add a comment » 

PHP UTF-8 cheatsheet

03 July 2006 - PHP

When we started building DropSend, we decided to support all languages worldwide from the start. The interface is currently in English only, but the application can send, store, sort and process your data whatever language you want. As a result, we have a good number of customers out east.

To support worldwide languages, you need to use UTF-8 encoding for your web pages, emails and application, rather than ISO 8859-1 or another common western encoding, since these don't support characters used in languages such as Japanese and Chinese.

Happily, UTF-8 is transparent to the core Latin characterset, so you won't need to convert all your data to start using UTF-8. But there are a number of other issues to deal with. In particular, because UTF-8 is a multibyte encoding, meaning one character can be represented by more one or more bytes. This causes trouble for PHP, because the language parses and processes strings based on bytes, not characters, and makes mincemeat multibyte strings - for example, by splitting characters 'in half', bodging up regular expressions, and rendering email unreadable.

There are a number of great articles online about UTF-8 and how it works - Joel Spolski's comes to mind - but very few about how to actually get it working with PHP and iron out all the bugs. So, here to save you the time we put in, is a quick cheatsheet and info about a few common issues.

Continued »  65 comments »  Add a comment » 

Optimising while() loops in PHP

02 July 2006 - PHP

Following the for() tests, I performed the same tests for while() loops. The speeds were very similar to the for() tests across the board, with just one syntax standing out: the one that doesn't use a comparison operator. It was about 50% faster than all the others:

$i = 1000000 ;
while($i){ $i-- ; }

As with the for() test, this is a count-down loop, which checks the boolean value of $i without a comparison operator for each iteration.

Continued »  2 comments »  Add a comment » 

JSON - better than XML?

02 July 2006 - JavaScript

JavaScript coders have been doing it for ages. Now it's got a name: JSON, or JavaScript Object Notation.

One of the sexier features of JavaScript is a shorthand notations for defining and populating a data structure containing objects and arrays. So, instead of:

me          = new Object() ;
me.name = 'Nick' ;
me.age = 30 ;
me.hobby = new Array()
me.hobby[0] = 'Reading' ;
me.hobby[1] = 'Writing' ;
me.smelly = false ;

You can do this...

Continued »  2 comments »  Add a comment » 

Optimising for() loops in PHP

01 July 2006 - PHP

I decided to try a couple of speed tests on for loops in PHP. The results were suprising. The speeds were almost exactly the same, with the exception of one syntax which was about 50% faster than the others, yet is possibly the least used. This is:

for($i=1000000; $i; $i--){}

It is a count-down loop, which checks the boolean value of $i without a comparison operator for each iteration.

Continued »  2 comments »  Add a comment » 

Trim a string in JavaScript

01 July 2006 - JavaScript

I've seen all kinds of techniques used to trim a string in JavaScript, some of them a bit bonkers. Here's the 'right' way:

var trimmed = str.replace(/^\s+|\s+$/g, '') ;

So there.

34 comments »  Add a comment » 

London Underground maps

01 June 2006 - Bits & bobs

A while ago I discovered a great reworking of the London Tube map by Orkar Karlin, which displayed visually the time to travel from place to place. This is a great concept, because for Londoners travelling on the Underground, nearness is about how long it takes to get somewere, not how far away it is, and all Londoners know there's not a lot of correlation between the two.

(Of course, the standard topological tube map doesn't tell how far away things are, or even where they are - only how to get there).

Today I found a couple of elegant interactive versions, by Tom Carden, which enable you to choose a start point and view travel times from there in different ways.


Add a comment » 

Visual complexity

01 June 2006 - Bits & bobs

A superb compilation of some of the best design mapping 'complex networks' - tube maps, genalogy charts and other network diagrams. Making complex information like this clear is a real artform: www.visualcomplexity.com.

I picked up this link from Alexander Cheek, who has has beautfully designed family tree. It is good to see print designers talking about usability.

Add a comment » 

AJAX, or just J?

01 June 2006 - JavaScript

"Nick, could we use some AJAX to do XYZ?"
"Sure. Or we could just use J. It's much quicker."

Continued »  Add a comment »