Can someone please help me - I need to write a function that will validate UK postcodes written in a textbox but every tutorial and example source code I find for it is in other languages such as VB and Javascript.

The VB code looks extremely small:

1.
Public Function isValidPostCode(ByVal input As String) As Boolean
2.

3.
Dim validInput As Boolean = False
4.

5.
' Check that the input is not null before checking
6.
If input IsNot Nothing Then
7.

8.
' Regex from:
9.
validInput = Regex.IsMatch(input, "^(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})$", RegexOptions.IgnoreCase)
10.

11.
End If
12.

13.
Return validInput
14.

15.
End Function

Is there a way this could be convereted for use in Delphi database?

Dani AI

Generated

A few practical options to finish this thread: wanted a Delphi version of the VB validator and already gave a solid rule-based routine if you prefer no external libs. If your Delphi does include a regular-expression unit (for example System.RegularExpressions or a PCRE/TPerlRegEx component) you can use a single, Delphi-friendly pattern that follows the BS7666 postcode rules and avoids .NET-only constructs like character-class subtraction.

uses
  System.RegularExpressions, SysUtils;

function IsValidUKPostcode(const Postcode: string): Boolean;
const
  Pattern = '^(GIR 0AA|[A-PR-UWYZ](?:\d{1,2}|[A-HK-Y]\d{1,2}|\d[A-HJKPSTUW])\s?\d[ABD-HJLNP-UW-Z]{2})$';
begin
  Result := TRegEx.IsMatch(Trim(Postcode), Pattern, [roIgnoreCase]);
end;

Notes and troubleshooting:

  • Normalize input (Trim, uppercase or use roIgnoreCase) and enforce a single space between outward and inward codes before saving to a database. Storing normalized values makes joins and lookups reliable.
  • This regex checks format only — it does not prove the postcode is allocated to an address. For authoritative validation use Royal Mail PAF or an address-lookup API.
  • If your Delphi build lacks System.RegularExpressions, use a PCRE/TPerlRegEx library or fall back to a structured check like the one offered (no dependencies, fast and explicit about excluded letters).
  • Test with typical examples: valid: "EC1A 1BB", "W1A 0AX", "M1 1AE", "B33 8TH", "CR2 6XH", "DN55 1PT". Invalid: "ZZ99 9ZZ", "A1", "12345".

This gives a compact, portable validator you can drop into a data-entry form or a DB layer and complements 's approach when a regex engine isn't available.

Recommended Answers

All 3 Replies

I don't think that Delphi has any function to parse regular expressions, so I looked on this site http://www.ml-consult.co.uk/foxst-39.htm and found some informations from which i built the following function

function TForm1.CheckIfValid(anInput : String) : Boolean;
var
  iSpacePos, i : byte;
  sInward, sOutward : String;
begin
  Result := False;
  if (anInput = EmptyStr) then Exit;
  anInput := UpperCase(anInput);
  iSpacePos := Pos(' ', anInput);
  if iSpacePos = 0 then Exit;
  sInward := Copy(anInput, iSpacePos + 1, 3);
  sOutward := Copy(anInput, 1, iSpacePos - 1);

  if StrToIntDef(sInward[1], -1) = -1 then Exit;  //first char has to be a number
  //these characters never appear in the inward code
  if (AnsiIndexStr(sInward[2], ['C', 'I', 'K', 'M', 'O', 'V']) <> -1) or
    (AnsiIndexStr(sInward[3], ['C', 'I', 'K', 'M', 'O', 'V']) <> -1) then Exit;
//build the outward code as the patterns from the site
  for i := 1 to Length(sOutward) do
    if ord(sOutward[i]) in [65..90] then // or sOutward[i] in ['A'..'Z']
      sOutward[i] := 'A'
    else sOutward[i] := '9';

  if AnsiIndexStr(sOutward, ['A9', 'A99', 'AA9', 'A9A', 'AA99', 'AA9A']) = -1 then Exit;
//the code is valid, so i return true value
  Result := true;

end;

If you want to create a more accurate validation, read the entire article from the link I posted.

Ionut

Thank you so very much, you have really helped me a lot.

You're welcome, but you have found your answer, please mark the thread as solved.

Ionut

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.