Sanity-checking UK postcode custom functions

One of our clients, a rather nice bunch of people called Hercules Site Services are having some FileMaker development work done.

As part of our work, we found the need to validate UK postcodes. For those of you not from this sceptred isle, a post code is similar to a ZIP code, in a specific format.

In order to verify the integrity of postcodes entered into the database, the developer (our very own Dr. Jon) has come up with a couple of neat custom functions.

We're releasing these as tweetware; that is, if you use them, tweet this page!

The first one checks that the postcode is valid. You can download it here or copy & paste:

/*
Custom function to check for a valid UK postcode — L[L]N[N] NLL

Dr Jonathan Jeffery 6/2/16
Released as tweetware
If you use it, Tweet it: http://bit.ly/1WgiVBX @igeekUK #filemaker

*/

Let([

// only keep alphanumeric characters
§Filtered = Filter ( Upper( Postcode ) ; "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) ;

// change all letters to L and all numbers to 0
§Encoded =
Substitute(
§Filtered ;
[ "A" ; "L" ] ;
[ "B" ; "L" ] ;
[ "C" ; "L" ] ;
[ "D" ; "L" ] ;
[ "E" ; "L" ] ;
[ "F" ; "L" ] ;
[ "G" ; "L" ] ;
[ "H" ; "L" ] ;
[ "I" ; "L" ] ;
[ "J" ; "L" ] ;
[ "K" ; "L" ] ;
[ "M" ; "L" ] ;
[ "N" ; "L" ] ;
[ "O" ; "L" ] ;
[ "P" ; "L" ] ;
[ "Q" ; "L" ] ;
[ "R" ; "L" ] ;
[ "S" ; "L" ] ;
[ "T" ; "L" ] ;
[ "U" ; "L" ] ;
[ "V" ; "L" ] ;
[ "W" ; "L" ] ;
[ "X" ; "L" ] ;
[ "Y" ; "L" ] ;
[ "Z" ; "L" ] ;
[ "1" ; "0" ] ;
[ "2" ; "0" ] ;
[ "3" ; "0" ] ;
[ "4" ; "0" ] ;
[ "5" ; "0" ] ;
[ "6" ; "0" ] ;
[ "7" ; "0" ] ;
[ "8" ; "0" ] ;
[ "9" ; "0" ]
);

§Inbound = Right( §Filtered ; 3 );
§Outbound = Left( §Filtered ; Length( §Filtered ) - 3 );

§TestGeneralFormat =
// Correct pattern of letters and numbers
Case(
// valid formats
§Encoded = "L0L0LL" ; 1;
§Encoded = "LL0L0LL" ; 1;
§Encoded = "L00LL" ; 1;
§Encoded = "LL00LL" ; 1;
§Encoded = "L000LL" ; 1;
§Encoded = "LL000LL" ; 1;

// otherwise fail
0
);

// Correct letter usage in Outbound section
§TestOutbound =
Case(
// not allowed to use Q,V or X in first position
not IsEmpty( Filter( Left( §Outbound ; 1 ) ; "QVX" ) ) ; 0 ;

// not allowed to use I,J or Z in second position
not IsEmpty( Filter( Middle( §Outbound ; 2 ; 1 ) ; "IJZ" ) ) ; 0 ;

// The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with L0L.
§Encoded = "L0L0LL" and IsEmpty( Filter( Middle( §Outbound ; 3 ; 1 ) ; "ABCDEFGHJKPSTUW" ) ) ; 0 ;

// The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with LL0L.
§Encoded = "LL0L0LL" and IsEmpty( Filter( Middle( §Outbound ; 4 ; 1 ) ; "ABEHMNPRVWXY" ) ) ; 0 ;

// otherwise OK
1
);

// Correct letter usage in Inbound section
§TestInbound =
Case(
// not allowed to use C,I,K,M,O or V
IsEmpty( Filter( §Inbound ; "CIKMOV" ) ) ; 1 ;

// otherwise fail
0
) ;

// final result
result =
Case(
// empty is OK
IsEmpty( §Encoded ) ; 1 ;

// all tests passed
§TestGeneralFormat + §TestOutbound + §TestInbound = 3 ; 1 ;

// otherwise fail
0
)

];

result

)

You may wish to use that function with this one, too, which ensures your postcode entries are 'clean'. Download from here.

/*
An auto-enter to keep postcodes clean (formatted as X[X]n[n] nXX):

Dr Jonathan Jeffery 6/2/16
Released as tweetware
If you use it, Tweet it: http://bit.ly/1WgiVBX @igeekUK #filemaker

*/


Let([

// get text with no spaces or other chars
§Input = Filter ( Upper( Self ) ; "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" );
§Length = Length(§Input);

// outbound section is 2-4 characters (letter [letter] number [number] )
§Outbound = Left( §Input ; §Length - 3 ) ;

// inbound section is always three characters (number letter letter)
§Inbound = Right( §Input ; 3 ) ;

result = §Outbound & " " & §Inbound

];

TextFormatRemove ( result )

)

Let us have your feedback - we're always interested in how people get on with any code we might release.



Make a comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

Note: All comments are moderated and will appear as soon as they are approved.