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 »
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 »
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.
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."

