Skip to content

Instantly share code, notes, and snippets.

@nery
Created February 20, 2014 17:17
Show Gist options
  • Save nery/9118763 to your computer and use it in GitHub Desktop.
Save nery/9118763 to your computer and use it in GitHub Desktop.
Canadian postal code validation
/*
Slightly strict regex check for validly formatted Canadian postal code
http://jsfiddle.net/7mBFf/
*/
var postal = new RegExp(/^\s*[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z](\s)?\d[a-ceghj-npr-tv-z]\d\s*$/i);
console.log(postal.test('m6k1s4')); // returns true
console.log(postal.test('M6K1S4')); // returns true
console.log(postal.test('90210')); // returns false
console.log(postal.test('d6k1s4')); // returns false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment