5

Is there a way to know where the blank space has to be, in an UK postal code, if the user doesn't write the postal code with the blank space?

For example, if the input is: EC1A1BB, W1A0AX, M11AE, B338TH, CR26XH, DN551PT then the output must be: EC1A 1BB, W1A 0AX, M1 1AE, B33 8TH, CR2 6XH, DN55 1PT.

Thank you

iusmar
  • 339
  • 1
  • 6
  • 15

4 Answers4

6

The space is always before the final three characters.

This graphic shows the format:

Format of a UK postcode

Source: GetTheData

Dan Winchester
  • 324
  • 1
  • 7
2

I've created a C# method to validate postcode:

private string ValidatePostcode(string postcode)
{
    if (!postcode.Substring(postcode.Length - 4, 1).Equals(" "))
    {
         postcode = postcode.Insert(postcode.Length - 3, " ");
    }
    return postcode;
}
iusmar
  • 339
  • 1
  • 6
  • 15
1

The space seems to fall before the last three characters. Postcodes always end with Digit-Letter-Letter.

1

The space is always before the NLL suffix. The prefix can be 3 or 4 characters before the space. LLNA_NLL. The above graphic is wrong! Should end NLL.

john
  • 11
  • 1