Validating Flight Codes

Working on a portal that allows end users to enter their flight details for Operation Wallacea (OPWALL) trips, we were finding many details not being entered correctly. Since the portal was to take some of the load off the operations staff, having to double-check everything was not really saving any tine.

The flight codes (or collaboratively, flight numbers) were just accepted as free-format, unvalidated strings. So let’s put some validation on that field. Easy. Well, kind of.

Search the Internet and you will find many posts on many sites that claim to provide the validation regular expression for flight codes. Most get some way there, but most miss out some subtlety of the format. This is may take on it – I can’t promise that I not missed out some additional detail, but I’m sure you will let me know if I have.

The Airline Codes Wikipedia page provides the format. The flight designator, aka flight code, aka flight number, follows this pattern:

xx(a)n(n)(n)(n)(a)

The key for this format is:

  • x = alphanumeric character: a-z, A-Z or 0-9
  • a = alphabetic character: a-z or A-Z
  • n = numeric digit: 0-9
  • (…) = the character in parenthesis is optional

The first two alphanumeric characters plus the third optional letter is know as the Airline Designator. The opti0nal letter is the Operation Suffix, but is generally not used due to lack of compliance of many major booking systems. But we need to take it into account just in case.

One thing the Wikipedia page does not mention, is the Airline Designator can be two letters, or a letter and a digit, but never two digits. So AA, A1 and 1A would pass general validation, but 11 would not.

So a regex (Regular Expression) validation (for PHP), split into sections, would be:

  • ([a-z][a-z]|[a-z][0-9]|[0-9][a-z])
  • [a-z]?
  • [0-9]{1,4}
  • [a-z]?

The regex would need to use the non-case sensitive option, assuming you have not already normalised the letters to upper- or lower-case. The complete validation check being:


if (preg_match('/^([a-z][a-z]|[a-z][0-9]|[0-9][a-z])[a-z]?[0-9]{1,4}[a-z]?$/i', $flight_code)) {
    //do passed-validation stuff
}

There will be ways of making the RE shorter, or cleverer, or more compact, but I value readability too much to even contemplate doing that. REs are hard enough to read as it is, so we’ll keep it as simple as we can.

The maximum length of a flight code is 8 characters, and the minimum is 3 characters, so there is a broad validation that can be run before the regex is invoked.

Of course, there is another approach to this whole thing: use an external database that lists all flight numbers, dates and times. Google provide an API to access this information, which we will explore as a future enhancements. An alternative flights API here.

2 Responses to Validating Flight Codes

  1. Marcel 2016-07-27 at 16:18 #

    Thank you. I think you’re missing a comma in preg_match(… , $flight_code).

    • Jason Judge 2016-07-27 at 16:22 #

      Certainly was. Thanks.

Leave a Reply