Validating uk postcodes

posted by | Leave a comment

To determine whether a postcode actually exists, you would need to access the Postcode Address File.(But even that wouldn't be a perfect check, as the PAF is never completely up to date; postcodes are constantly being added, and existing premises are occasionally re-coded.) You can improve the accuracy of the validation by checking the first one or two letters against a lookup table of the 124 postal areas (which can be found at int/post_code/en/countries/GBR.pdf). FUNCTION Check Postcode * Checks that a UK postcode is syntactically valid.Essentially, every postcode consists of two elements, separated by a space.The outward part of the code comes first, then the space, then the inward part of the code.

The inward code always consists of exactly one digit followed by exactly two letters.

Feel free to copy and paste it into your application. lc Test = ; CHRTRAN(tc Code, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", ; REPLICATE("A", 26)) lc Test = ; CHRTRAN(lc Test, "0123456789", ; REPLICATE("9", 10)) * Separate out the test string into outward and * inward portions lc Outward = ALLTRIM(LEFT(lc Test, LEN(tc Code)-4)) lc Inward = RIGHT(lc Test, 3) * The inward portion must be in the format 9AA IF lc Inward CHRTRAN(lc In Letters, "CIKMOV", "") RETURN . ENDIF * The outward portion must be in one of the * following formats: * A9, AA9, A99, AA99, A9A, AA9A IF NOT INLIST(lc Outward, ; "A9", "AA9", "A99", "AA99", "A9A", "AA9A") RETURN . It then become an easy matter to test the code against a list of valid patterns.

To use it, simply call the Check Postcode function, passing the postcode as a parameter. This form of pattern matching can be applied to many other types of validation.

In theory it should as the space is part of the spec.

I'd hate my code to unexpectedly break due to a valid bug fix.

Leave a Reply

operaupdating ru