Skip to content

Instantly share code, notes, and snippets.

@kimek
Created May 23, 2017 16:46
Show Gist options
  • Save kimek/b59617bd04fafad160f8ed8448919af3 to your computer and use it in GitHub Desktop.
Save kimek/b59617bd04fafad160f8ed8448919af3 to your computer and use it in GitHub Desktop.
Get first postcode part from UK postcode with vanilla javascript
function postcodePart($postcode) { //https://www.mrs.org.uk/pdf/postcodeformat.pdf
var $postcodePart;
switch($postcode.length) {
case 5:
$postcodePart = $postcode.substr(0,2)
break;
case 6:
$postcodePart = $postcode.substr(0,3)
break;
case 7:
$postcodePart = $postcode.substr(0,4)
break;
default:
$postcodePart = $postcode;
break;
}
return $postcodePart;
}
function postcodePart($postcode) { //https://www.mrs.org.uk/pdf/postcodeformat.pdf
var $postcodePart;
switch($postcode.length) {
case 5:
$postcodePart = $postcode.substr(0,2)
break;
case 6:
$postcodePart = $postcode.substr(0,3)
break;
case 7:
$postcodePart = $postcode.substr(0,4)
break;
default:
$postcodePart = $postcode;
break;
}
return $postcodePart;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment